PHP, need to subtract 12 hours and 30 minutes from a DateTime - php

I have a PHP DateTime variable.
How can I reduce or subtract 12hours and 30 minutes from this date in at PHP runtime?

Subtract 12 Hours and 30 minutes from a DateTime in PHP:
$date = new DateTime();
$tosub = new DateInterval('PT12H30M');
$date->sub($tosub);
The P stands for Period. The T stands for Timespan.
See DateTime, DateTime::sub, and DateInterval in the PHP manual. You'll have to set the DateTime to the appropriate date and time, of course.

Try with:
$date = new DateTime('Sat, 30 Apr 2011 05:00:00 -0400');
echo $date->format('Y-m-d H:i:s') . "\n";
$date->sub(new DateInterval('PT12H30M'));
echo $date->format('Y-m-d H:i:s') . "\n";
//Result
2011-04-30 05:00:00
2011-04-29 16:30:00

Try strtotime() function:
$source_timestamp=strtotime("Sat, 30 Apr 2011 05:00:00 -0400");
$new_timestamp=strtotime("-12 hour 30 minute", $source_timestamp);
print date('r', $new_timestamp);

Maybe it will be useful for some cases
$date = new DateTime();
$date->modify('-12 hours -30 minutes');
echo $date->format('H:i:s');

try using this instead
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));

If you are not so familiar with the spec of DateInterval like PT12H30M you can proceed with more human readable way using DateInterval::createFromDateString as follows :
$date = new DateTime();
$interval = DateInterval::createFromDateString('12 hour 30 minute');
$date->sub($interval);
Or with direct interval in sub function like below :
$date = new DateTime();
$date->sub(DateInterval::createFromDateString('12 hour 30 minute'));

Store it in a DateTime object and then use the DateTime::sub method to subtract the timespan.

I used in one line, for 12 hours only, and just as an hour display
$date = new DateTime(); $date->modify('-12 hours'); echo $date->format('H')-0;
I used the -0 since sometimes it put a 0 in front of the digit unless I done that, strange.

Here is detailed description of date function,

Using simply strtotime
echo date("Y-m-d H:i:s",strtotime("-12 hour -30 minutes"));
Using DateTime class
$date = new DateTime("-12 hour -30 minutes");
echo $date->format("Y-m-d H:i:s");

Related

PHP convert the day date with the time

I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.
Code:
<?php
$get_time1 = '06:00:00';
$get_time2 = '23:00:00';
date_default_timezone_set('Europe/London');
$Date = date('Y-m-d');
$time = date('H:i:s');
?>
Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?
Thank you.
This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...
$date = DateTime::createFromFormat("H:i:s", $get_time2);
if ( $date < new DateTime() ) {
$date->modify("+1 day");
}
which gives
2019-11-27 23:00:00
and for $get_time1...
2019-11-28 06:00:00
If you use a DateTime that will allow you to do date arithmetic.
$now = new DateTime();
$tomorrow = $now->modify("+1 day");
You can also use strtotime to get a unix timestamp as explained in this answer.
$tomorrow = strtotime('+1 day');
maybe this will do?
$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);
echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));

converting UNIX time stamp date to the Monday of that week

how can i convert a time stamp to Monday corresponding with that week?
example:
1473750000 (Tue, 13 Sep 2016 07:00:00 GMT)
// coding magic
1473638400 (Mon, 12 Sep 2016 00:00:00 GMT )
To convert timestamps, you can use a PHP Function gmdate(). I already use this on some of my projects/sites.
$timestamp = strtotime("+1 hour"); //Uk Time
echo gmdate("H:i", $timestamp); //Hour:Minutes
For the other parts of gmdate(), check the PHP Manual.
I also add my solution to the problem, using DateInterval.
function getLastMonday($timestamp){
// Initialize DateTime object
$date = new DateTime();
$date->setTimestamp($timestamp);
// Calculate num of days to substract and create date interval
$dayOfWeek = $date->format("w");
$interval = new DateInterval("P".(($dayOfWeek+6)%7).'D');
$interval->invert = 1;
// Substract the Date interval
$date->add($interval);
return $date;
}
I've found an answer using the documentation provided in the answers:
$date = date('Y-m-d',strtotime('this monday this week',1473750000));

PHP Reduce days, hours and minutes from a datetime

Need to reduce no of days, hours and minutes from a datetime using php.
Datetime is of the format Y-m-d H:i:s
eg: Suppose datetime is 2013-03-20 14:20:00. How to reduce 2 days , 3 hours and 10 minutes from this such that it results in 2013-03-18 11:10:00.
<?php
$date = new DateTime("2013-03-20 14:20:00");
$dateIncremented = $date->sub(date_interval_create_from_date_string('2 days 3 hours 10 minutes'));
$finalDate = $date->format("Y-m-d H:i:s");
echo $finalDate;
?>
Allirght. An Alias. But Readable format
use the DateTime object :
$date = new DateTime('2013-03-20 14:20:00');
$date->sub(new DateInterval('P2DT3H10M'));
echo $date->format('Y-m-d H:i:s');
You must explore DateTime::sub and DateInterval, and about DateInterval format
echo \DateTime::createFromFormat('Y-m-d H:i:s', '2013-03-20 14:20:00')
->sub(new \DateInterval('P2DT3H10M'))
->format('Y-m-d H:i:s'); // 2013-03-18 11:10:00

PHP get 31 days distance from starting date

How can I get what date it will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.
Thank you.
strtotime will give you a Unix timestamp:
$date = '20101007';
$newDate = strtotime($date.' + 31 days');
you can then use date to format that into the same format, if that's what you need:
echo date('Ymd', $newDate);
If you're using PHP 5.3:
$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');
The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. http://us3.php.net/manual/en/book.datetime.php
Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.
echo date('Y m d',strtotime('+31 Days'));

PHP Date Time Current Time Add Minutes

Simple question but this is killing my time.
Any simple solution to add 30 minutes to current time in php with GMT+8?
I think one of the best solutions and easiest is:
date("Y-m-d", strtotime("+30 minutes"))
Maybe it's not the most efficient but is one of the more understandable.
This is an old question that seems answered, but as someone pointed out above, if you use the DateTime class and PHP < 5.3.0, you can't use the add method, but you can use modify:
$date = new DateTime();
$date->modify("+30 minutes"); //or whatever value you want
Time 30 minutes later
$newTime = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +30 minutes"))
$timeIn30Minutes = mktime(idate("H"), idate("i") + 30);
or
$timeIn30Minutes = time() + 30*60; // 30 minutes * 60 seconds/minute
The result will be a UNIX timestamp of the current time plus 30 minutes.
echo $date = date('H:i:s', strtotime('13:00:00 + 30 minutes') );
13:00:00 - any inputted time
30 minutes - any interval you wish (20 hours, 10 minutes, 1 seconds etc...)
It looks like you are after the DateTime function add - use it like this:
$date = new DateTime();
date_add($date, new DateInterval("PT30M"));
(Note: untested, but according to the docs, it should work)
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->modify("+10 minutes")->format("H:i:s A");
$ck=2016-09-13 14:12:33;
$endtime = date('H-i-s', strtotime("+05 minutes", strtotime($ck)));
In addition to Khriz's answer.
If you need to add 5 minutes to the current time in Mysql format you can do:
$cur_time=date("Y-m-d H:i:s");
$duration='+5 minutes';
echo date('Y-m-d H:i:s', strtotime($duration, strtotime($cur_time)));
time after 30 min, this easiest solution in php
date('Y-m-d H:i:s', strtotime("+30 minutes"));
for DateTime class (PHP 5 >= 5.2.0, PHP 7)
$dateobj = new DateTime();
$dateobj ->modify("+30 minutes");
The question is a little old, but I come back to it often ;p
Another way, which is also a one liner:
<?= date_create('2111-11-11 00:00:00')->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
Or from timestamp, returns Y-m-d h:i:s:
<?= date_create('#'.time())->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
Or from timestamp, returns timestamp:
<?= date_create('#'.time())->modify("+30 minutes")->format('U') ?>
new DateTime('+30minutes')
As simple as the accepted solution but gives you a DateTime object instead of a Unix timestamp.
$time = strtotime(date('2016-02-03 12:00:00'));
echo date("H:i:s",strtotime("-30 minutes", $time));

Categories