使用php为日期时间添加时间

时间:2017-01-22 06:00:04

标签: php date time

我在数据库startdate(datetime),global_time(时间),enddate(datetime)中有3个字段.Start date将是今天的datetime。

$start_date = date('Y-m-d h:i:s'); 
$global_time = '00:45:30'   // comes from database global_time

现在我要总结$ start_date和$ global_time并存储到数据库字段enddate.Can有人建议我怎么做?

1 个答案:

答案 0 :(得分:0)

$start_date  = new DateTime('2017-01-22 05:05:00');
$global_time = "00:45:30";
sscanf($global_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;//Converted in to seconds
$newtime="PT".$time_seconds."S";
$start_date->add(new DateInterval($newtime));
$enddate = $start_date->format('Y-m-d H:i:s');
echo $enddate;
PT0H45M30S for a interval of 0 hour, 45 minutes and 30 seconds.