差異正在返回一個不正確的值,不正確的意思是它不是預期的。我需要它返回總分鐘數,例如1小時7分鐘需要67分鐘。diff爲什麼會返回不正確的值?
<?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
謝謝,我更新了它。 – Edward
那麼,現在你看到了嗎? –
那麼如何顯示總分鐘數,例如67分鐘?謝謝! – Edward