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

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

我的 SQL 数据库中有 2 个 table。一个叫做 dungeons,另一个叫做 dungeonruns

dungeons table 中有一行叫做 time 它是 SQL 时间格式 00:00:00。在 dungeonruns table 中有一个名为 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');

Demo

基本上,要将该时间添加到该日期时间值,您需要将其转换为 DateInterval,然后您可以将其添加到 DateTime 对象。