Hi I have a date string taken from database. And it's like
$date = '2016-03-21 11:00:00';
And I have a time string taken from database.
$time = '02:30:00'
Is there a way to add these two and get like these in PHP?
$newtime = '2016-03-21 13:30:00';
Use preg_replace to transform your $time value:
$date = '2016-03-21 11:00:00';
$time = '02:30:00';
$str = "$date + ".preg_replace( '/:(\d+):(\d+)$/',' hours, \1 minutes, \2 seconds',$time );
$date = new DateTime( $str );
echo $date->format('Y-m-d H:i:s');
Will print:
2016-03-21 13:30:00
With preg_replace we transform 02:30:00 in 02 hours, 30 minutes, 00 seconds and, postponing it to $date, we obtain this string:
2016-03-21 11:00:00 + 02 hours, 30 minutes, 00 seconds
directly accepted by DateTime class.
Read more about DateTime
I'm sure there's a better way to do this but:
$date = new DateTime($time);
$int = new DateInterval($date->format("\P\TH\Hi\Ms\S"));
$dateTime = new DateTime($date);
return $dateTime->add($int);
Related
I want to have have 2 variables with the current time in hours:minutes and also one that has +15 minutes. But first I must also add +6 hours.
So for example, right now it is 2018-01-07 16:35:10. So first I add +6 hours. So it will be 2018-01-07 22:35:10. Next, I want to extract only the hours:minutes.
I want to get only "22:35" to variable.
And next variable, I want 22:35 +15 minutes, so 22:50.
So I have $dateNow = 22:35 and $dateThen = 22:50
I have tried this so far to get current time now and +6 hours, but it's not working. Error: Call to a member function format() on integer
$now = strtotime(date("Y-m-d H:i:s")." +6 hours");
$then = $now->format('H:i');
echo $then;
i think in this case it would be very use full to use the DateTime class from PHP. The Problem with your code is strtotime returns a int not an DateTime object.
I've modified your code so it will work:
$org = new DateTime("2018-01-07 16:35:10");
$then = $org->add(new DateInterval("PT6H"));
echo $then->format("H:i"),"<br>";
$afterThen = $then->add(new DateInterval("PT15M"));
echo $afterThen->format("H:i");
Short solution with DateTime and DateInterval objects:
$now = new DateTime();
$result = $now->add(new DateInterval('PT6H15M'))->format('H:i');
Try this:
$time = time() + 3600 * 6; // add 6 hours
$date = date("H:i", $time); // format date
$date_plus_15 = date("H:i", $time + 60 * 15); // format date and add 15 minutes
echo "Time: {$date} <br>";
echo "Time + 15 mins: {$date_plus_15}";
Example here: https://ideone.com/ZR6cfy
I have a date time in 'Y-m-d H:i:s', and i tried to substract the now date with the defined date +1 day to get remaining time in hours, minutes and seconds:
$time = '2017-10-05 14:54:03';
$now = date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
$dteDiff = $endTransaction - $now;
echo $dteDiff;
but i always get 0 as the result
You are doing it wrong. The date function returns string so PHP is not able to compare anything. Try with the DateTime class instead. Its diff method returns the DateInterval object with some public properties, like the days property among others, which is the positive integer number (rounded down) of days between two dates:
$now = new \DateTime();
$endTransaction = (new \DateTime('2017-12-05 14:54:03'))->modify('+1 day');
$diff = $endTransaction->diff($now);
printf(
'Difference in days: %d, hours: %d, minutes: %d, seconds: %d',
$diff->days,
$diff->h,
$diff->m,
$diff->s
);
You probably need to use this date_diff
$time = '2017-10-05 14:54:03';
$now = date_create(date('Y-m-d H:i:s'));
$endTransaction = date_create(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
$dteDiff = date_diff($now, $endTransaction);
$date = new DateTime($dteDiff);
$result = $date->format('Y-m-d H:i:s');
According to above mentioned description please try executing following code snippet as a solution to it.
$time = '2017-10-05 14:54:03';
$now = strtotime(date('Y-m-d H:i:s'));
$endTransaction = strtotime(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
$dteDiff = ($endTransaction - $now)/(24*60*60);
echo round($dteDiff);
$endTransaction and $now are strings.
$time = '2017-10-05 14:54:03';
$now = date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
echo($endTransaction."\n");
echo($now."\n");
It prints:
2017-10-06 14:54:03
2017-10-05 11:45:39
The subtraction is not a valid operation for strings. It can handle only numbers. The strings above are converted to numbers. The conversion uses only the leftmost digits present in the string, until it reaches the first character that is not a digit.
Both strings above produce 2017 when they are converted to numbers and their difference is, of course, 0.
The easiest way to work with dates in PHP is to use the DateTime and its related classes.
// Convert the input string to a DateTime object
$then = new DateTime('2017-10-05 14:54:03');
// Add 1 day
$then->add(new DateInterval('P1D'));
// Get the current date and time
$now = new DateTime('now');
// Compute the difference; it is a DateInterval object
$diff = $now->diff($then);
// Display the dates and the difference
echo('Then: '.$then->format("Y-m-d H:i:s\n"));
echo('Now : '.$now->format("Y-m-d H:i:s\n"));
echo('Remaining: '.$diff->format("%R%a days, %h hours, %i minutes, %s seconds.\n"));
The output:
Then: 2017-10-06 14:54:03
Now : 2017-10-05 12:36:25
Remaining: +1 days, 2 hours, 17 minutes, 38 seconds.
I have this datetime code below. I want to subtract 23 hours and 59 minutes in $begin variable:
$date = 2016-03-14 23:59:59;
$given = new DateTime($date, new DateTimeZone("Asia/Tokyo"));
$given->setTimezone(new DateTimeZone("UTC"));
$end = $given->format("Y-m-d H:i:s");
$begin = date('Y-m-d H:i:s',strtotime('-23 hours',strtotime($end)));
Now The output is:
$end is: 2016-03-14 14:59:59
$begin is: 2016-03-13 15:59:59
The output I want is:
$end is: 2016-03-14 14:59:59
$begin is: 2016-03-13 15:00:00
How can I subtract the minutes in seconds in begin? Or there is a best way to do it?
As you just want to change the hour, you can make this to get the desire result, but you also can do something else like Amit Roy did or ceejayoz suggest. This is a pretty simple solution, just do it: reset the min and sec to 00 so that your output shows like you want.
echo $begin = date('Y-m-d H:00:00',strtotime('-23 hours', strtotime($end)));
Use the DateInterval class with the DateTime::sub method.
Example:
$begin_datetime = $given->sub(new DateInterval('PT23H59M00S'));
$begin = $begin_datetime->format('Y-m-d H:i:s');
http://php.net/manual/en/datetime.sub.php
http://php.net/manual/en/dateinterval.construct.php
[As Frayne make the comment.] As you just want to change the hour, not the min or sec, you have to set the min and sec to 00:00 as your desire output.
echo $begin = date('Y-m-d H:00:00',strtotime('-23 hours', strtotime($end)));
You can did this thing in many different ways, and this is one way.
You have to do this to get desired result. You were subtracting hours alone
$begin = date('Y-m-d H:i:s',strtotime('-23 hours',strtotime($end)));
You have to subtract min and sec too
$begin = date('Y-m-d H:i:s',strtotime('-23 hours -59 minutes -59 seconds',strtotime($end)));
Finally you get this code:
$date = "2016-03-14 23:59:59";
$given = new DateTime($date, new DateTimeZone("Asia/Tokyo"));
$given->setTimezone(new DateTimeZone("UTC"));
$end = $given->format("Y-m-d H:i:s");
echo $begin = date('Y-m-d H:i:s',strtotime('-23 hours -59 minutes -59 seconds',strtotime($end))); //2016-03-13 15:00:00
Here's an example of the datetime strings I am working with:
Tue May 15 10:14:30 +0000 2012
Here is my attempt to add an hour onto it:
$time = 'Tue May 15 10:14:30 +0000 2012';
$dt = new DateTime($time);
$dt->add(new DateInterval('P1h'));
But the second line gives the error that it couldn't be converted.
Thanks.
You should add a T before the time specification part:
$time = 'Tue May 15 10:14:30 +0000 2012';
$dt = new DateTime($time);
$dt->add(new DateInterval('PT1H'));
See the DateInterval constructor documentation:
The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.
(Emphasis added)
Previous answers works fine. However, I usually use datetime modify in my externally hosted websites. Check php manual for more information.
With the code proposed, it should work like this:
$time = 'Tue May 15 10:14:30 +0000 2012';
$dt = new DateTime($time);
$dt->modify('+ 1 hour');
For those not using object orientation, just use it this way (first line DateTime just to bring somethng new to this thread, I use it to check server time):
$dt = new DateTime("#".$_SERVER['REQUEST_TIME']); // convert UNIX epoch to PHP DateTime
$dt = date_modify($dt, "+1 hour");
Best,
Using strtotime():
$time = 'Tue May 15 10:14:30 +0000 2012';
$time = strtotime($time) + 3600; // Add 1 hour
$time = date('D M j G:i:s O Y', $time); // Back to string
echo $time;
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");