您也可以使用以下方法将时间转换为秒,该方法来源于:
将HH:MM:SS格式的时间转换为仅秒?
$time = '03:05:01';
$seconds = strtotime("1970-01-01 $time UTC");
然后您可以将秒数添加到:
$currentTime = '2015-10-10 13:40:14';
$newTime = date("Y-m-d H:i:s", strtotime( $currentTime.'+'.$seconds.' seconds'));
如果你更喜欢使用@John Conde提供的DateTime对象,有两种方法将时间字符串转换成格式:
$formattedTime = preg_replace("/(\d{2}):(\d{2}):(\d{2})/","PT$1H$2M$3S","03:05:11");
或者,当你从数据库中读取它时:
select concat(hour(last_modified),'H',minute(last_modified),'M',second(last_modified),'H') from people;
因此,更通用的代码方法如下:
$initial = 'some time';
$interval = 'the interval value';
$initialTime = new DateTime($initial);
$intervalTime = new DateInterval($interval);
$initialTime->add($intervalTime);
echo $initialTime->format('Y-m-d H:i:s');