How do you calculate the end time depending on the time and duration of the start?

I create an event calendar and pass the PHP start time, in the format 2009-09-25 15:00:00. The duration also passes, which can be in the format of 60 minutes or 3 hours. Converting from hours to minutes is not a problem. How to add a length of time to a set start point to properly format the end time?

+3
source share
3 answers

Using strtotime () , you can convert the current time (2009-09-25 15:00:00) to a timestamp, and then add (60 * 60 * 3 = 3 hours) to the timestamp. Finally, just convert it back anytime.

//Start date
$date = '2009-09-25 15:00:00';
//plus time
$plus = 60 * 60 * 3;
//Add them
$time = strtotime($date) + $plus;
//Print out new time in whatever format you want
print date("F j, Y, g:i a", $time);
+7
source

An easy way if you have a sufficiently high version number:

$when = new DateTime($start_time);
$when->modify('+' . $duration);
echo 'End time: ' . $when->format('Y-m-d h:i:s') . "\n";
+9
source

As a complement to the answer, @Xeoncross good strtotime strtotime()supports formats such as "2009-09-25 +3 hours", "September 25 +6 days", "next Monday", "last Friday", "-15 minutes", etc. .d.

0
source

Source: https://habr.com/ru/post/1715826/


All Articles