簡體   English   中英

PHP DateTime:如何使用24小時時鍾格式兩次獲取“ diff”

[英]PHP DateTime: How to get “diff” between two times using 24 hour clock format

嘗試減去兩次:

$obtime = "2310";
$runtime = "0048";

$run=new DateTime($runtime);
$ob=new DateTime($obtime);
$interval= $run->diff($ob);
$age=$interval->format('%H%I');
print "Age is: $age\n";

上面將輸出2222,意味着相差22小時22分鍾。 當然,我們知道時鍾會前進,而0048就是2310之后38分鍾的1小時。

有沒有更好的方法來找到兩個“ 24小時”時間之間的時差?

怎么樣

$obtime = strtotime("2310");
$runtime = strtotime("0048");
echo gmdate("H:i:s", $obtime - $runtime);

謝謝gwillie。 那沒有像您所擁有的那樣起作用,而是因為時間變量的順序。

如果您從較新時間中減去較舊時間,它將起作用。

輸出1小時38分鍾:

$obtime = strtotime("2210");
$runtime = strtotime("2348");
echo gmdate("H:i", $runtime - $obtime);

輸出22小時22分鍾時:

$obtime = strtotime("2210");
$runtime = strtotime("2348");
echo gmdate("H:i", $obtime - $runtime);

很高興知道此方法可以前進或后退,而我使用的原始方法並不是那么“智能”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM