[英]PHP: Get difference between two DateTime objects in Hours, Minutes, and Seconds
[英]time difference between two datetime only in hours
如何僅以小時為單位獲取兩個日期時間之間的時差
我的代碼是
$datetime1 = new DateTime('2017-02-06 04:00:00 AM');
$datetime2 = new DateTime('2017-02-07 04:10:00 AM');
$interval = $datetime2->diff($datetime1);
echo $interval->format('%d')." Days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
我的輸出是1天0小時10分鍾
但我想要這樣的輸出:“ 24小時10分鍾”
不要單獨打印日期,將其乘以24並將其加到小時數中。
echo 24*$interval->format('%d') + $interval->format('%h') . " Hours " . $interval->format('%i') . " Minutes";
$interval = ($datetime2 - $datetime1 )/60/60;
回聲$間隔;
還有一個
echo round((strtotime($datetime1) - strtotime($datetime2))/3600, 1);
也嘗試一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.