将迄今为止的小时数通过“;变量“;在php中不是静态的


Add number of hours to date through "variable" in php not statically

我已经将日期和时间存储在数据库中,如2015-06-31 14:00。我想通过变量向date字段添加一些hours。我如何添加

<?php
//set an date and time to work with
$start = '2015-06-31 14:00';
$hours='05:00';
//display the converted time
echo date('Y-m-d H:i',strtotime("+{$hours} hours",strtotime($start)));
?>

试试这个

<?php
$start = '2015-10-31 14:00';
$hours = '05:00';
$date = new DateTime($start);
$date->modify(sprintf("+%d hours", $hours));
echo $date->format("Y-m-d H:i");
?>

输出:

2015-10-31 19:00

检查此

//set an date and time to work with
$start = '2015-06-30 14:00';
$hours= 5;
//display the converted time
echo date('Y-m-d H:i',strtotime("+{$hours} hours",strtotime($start)));

输出为2015-06-30 19:00