How do you calculate an end time based on start time and duration?

邮差的信 提交于 2019-12-06 02:53:31

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

//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);

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";

As an addendum to @Xeoncross's good strtotime answer, strtotime() supports formats like "2009-09-25 +3 hours", "September 25 +6 days", "next Monday", "last Friday", "-15 minutes", etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!