将日期和时间存储为2个单独字符串的日期添加2小时

时间:2013-04-28 23:13:45

标签: php

$date = $feed[$x]['date'];    // Date string = 04-29-2013
$time = $feed[$x]['time'];    // Time string = 11:30pm

我需要增加2小时的时间,但日期也必须正确。

如何为此字符串添加2小时或我需要添加x小时。

1 个答案:

答案 0 :(得分:4)

$date = '04-29-2013';
$time = '11:30pm';

$compound = $date . ' ' . $time;

$dt = DateTime::createFromFormat('m-d-Y h:ia', $compound);
$dt->modify('+2 hour');

$newDate = $dt->format('m-d-Y');
$newTime = $dt->format('h:ia');

var_dump($newDate, $newTime);

在线演示:http://ideone.com/x4WgDV