我的日期和时间格式是:
示例1:
$dateTime = 2016-09-08 21:00; //(year-month-day hour:minute)
$interval = 04:00; //(hour:minute)
$outputShouldBe = 2016-09-09 01:00;
示例2:
$dateTime = 2016-09-08 21:00; //(year-month-day hour:minute)
$interval = 01:40; //(hour:minute)
$outputShouldBe = 2016-09-08 22:40;
答案 0 :(得分:2)
$dateTime = '2016-09-08 21:00';
echo date( "Y-m-d h:i", strtotime( "2016-09-08 21:00 +4 hours" ) );
echo date( "Y-m-d H:i", strtotime( "2016-09-08 21:00 +1 hours 40 minutes" ) );
答案 1 :(得分:1)
您还可以使用DateTime
和DateInterval
来完成任务。如果您愿意,可以缩短代码。但它更具可读性。
$dt = new Datetime('2016-09-08 21:00');
$interval = new DateInterval('PT4H');
$dt->add($interval);
echo $dt->format('Y-m-d H:i');
$dt = new Datetime('2016-09-08 21:00');
$interval = new DateInterval('PT1H40M');
$dt->add($interval);
echo $dt->format('Y-m-d H:i');
答案 2 :(得分:0)
$dateTime = '2016-09-08 21:00';
$intervalhr = 4;
$intervalmin= 0;
//display the converted time
echo date('Y-m-d H:i',strtotime('+'.$intervalhr .' hour ',strtotime($dateTime)));
你可以通过给出小时整数和相同的分钟来实现