Diff 返回的值不正确,不正确意味着它不是预期的。我需要它返回总分钟数,例如 1 小时 7 分钟需要 67 分钟。
<?php
// Trouble-shoot why time is being calculate incorrectly at 7 minutes when
// it should be 67 minutes for total travel time.
date_default_timezone_set('America/New_York');
// Display the total travel time in minutes for a trip.
$depart = "10:31:00 07/30/2013";
$arrival = "7/30/2013 11:38:00 AM";
echo "depart time is: " . $depart . "\n";
echo "arrival time is: " . $arrival . "\n";
$depart_raw = DateTime::createFromFormat("H:i:s m/d/Y", $depart);
$arrival_raw = DateTime::createFromFormat("m/d/Y g:i:s a", $arrival);
var_dump($depart_raw);
var_dump($arrival_raw);
$interval = $depart_raw->diff($arrival_raw);
var_dump($interval);
echo "Total travel time is: " . $interval->format("%i")." minutes" . "\n";
?>
输出:
depart time is: 10:31:00 07/30/2013
arrival time is: 7/30/2013 11:38:00 AM
object(DateTime)#1 (3) {
["date"]=>
string(19) "2013-07-30 10:31:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/New_York"
}
object(DateTime)#2 (3) {
["date"]=>
string(19) "2013-07-30 11:38:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/New_York"
}
object(DateInterval)#3 (8) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(0)
["h"]=>
int(1)
["i"]=>
int(7)
["s"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(0)
}
Total travel time is: 7 minutes