当前时间(PHP) + SQL时间= AS SQL时间戳


Current Time(PHP) + SQL time = AS SQL Timestamp

我有2个表在我的SQL数据库。一个叫dungeons,另一个叫dungeonruns

dungeons表中有一行称为time,它的SQL时间格式为00:00:00。dungeonruns表中有一行stoptime,时间戳格式为0000-00-00 00:00:00。

我想让我的PHP脚本从dungeons,添加到当前时间,然后保存在stoptime行时间。

这是我尝试过的:

$stoptime = date('Y-m-d H:i:s') + $time;
//$time is the time from the DB and its the 00:00:00 format.
$mysqlDateTime = '2015-09-01 00:00:00';
$timeToAdd     = '12:30:00';
$timeParts      = explode(':', $timeToAdd);
$intervalString = sprintf('PT%dH%dM%dS', $timeParts[0], $timeParts[1], $timeParts[2]);
$stopTime       = (new DateTime($mysqlDateTime))->add(new DateInterval($intervalString))->format('Y-m-d H:i:s');

演示

基本上,要将时间添加到datetime值中,您需要将其转换为DateInterval,然后将其添加到datetime对象中