如何根据开始时间和持续时间计算结束时间?

Cha*_*lts 3 php time datetime duration

我正在构建一个事件日历并将开始时间传递给PHP,格式为2009-09-25 15:00:00.持续时间也会通过,可能是60分钟或3小时的格式.从小时转换为分钟不是问题.如何为确定的起点添加一段时间以正确格式化结束时间?

cha*_*aos 9

如果您有足够高的版本号,可以轻松实现:

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


Xeo*_*oss 7

使用strtotime(),您可以将当前时间(2009-09-25 15:00:00)转换为时间戳,然后将(60*60*3 = 3小时)添加到时间戳.最后只需将其转换回您想要的任何时间.

//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);
Run Code Online (Sandbox Code Playgroud)