-5

使用DateTime::createFromFormat如何计算与较晚时间之间的分钟$dt1数差异$dt2$dt2

我试过$dt2 - $dt1了,但这当然会产生一个error

<?php
date_default_timezone_set('America/New_York');

// $time1 will always be in HH:MM:SS MM/DD/YYYY format.
$time1 = "08:18:00 07/11/2013";

// $time2 will always be in MM/DD/YYYY HH:MM:SS format.
$time2 = "7/11/2013 9:11:00 AM";

$dt1 = DateTime::createFromFormat("H:i:s m/d/Y", $time1);
$dt2 = DateTime::createFromFormat("m/d/Y g:i:s a", $time2);
var_dump($dt1);
var_dump($dt2);
if ($dt1 < $dt2) echo "The first is earlier" . "\n";

?>
4

2 回答 2

2
$time1 = "08:18:00 07/11/2013";
$time2 = "7/11/2013 9:11:00 AM";
$dt1 = DateTime::createFromFormat("H:i:s m/d/Y", $time1);
$dt2 = DateTime::createFromFormat("m/d/Y g:i:s a", $time2);
$interval = $dt1->diff($dt2);
echo $interval->format("%i")." minutes";
于 2013-07-12T07:33:10.797 回答
0

看看DateTime::diff()

DateTime::diff-- DateTimeImmutable::diff-- DateTimeInterface::diff-- date_diff-- 返回两个 DateTime 对象之间的差异

于 2013-07-12T07:28:44.890 回答