簡體   English   中英

計算laravel中兩個日期之間的小時數、分鍾數和秒數

[英]Calculate number of hours , minutes and seconds between two dates in laravel

假設我們必須像這樣使用 DateTime 格式化日期:

$started_at = '2016-07-05 12:29:16';
$ended_at = '2016-07-06 13:30:10';

現在我想像這樣計算它們的小時數、分鍾數和秒數:

15 hours and 50 minutes and 15 seconds

我怎樣才能在 laravel 和使用Carbon中以最簡單的方式做到這一點。

嘗試這個

$t1 = Carbon::parse('2016-07-05 12:29:16');
$t2 = Carbon::parse('2016-07-04 13:30:10');
$diff = $t1->diff($t2);

這會給你

DateInterval {#727
     +"y": 0,
     +"m": 0,
     +"d": 0,
     +"h": 22,
     +"i": 59,
     +"s": 6,
     +"weekday": 0,
     +"weekday_behavior": 0,
     +"first_last_day_of": 0,
     +"invert": 1,
     +"days": 0,
     +"special_type": 0,
     +"special_amount": 0,
     +"have_weekday_relative": 0,
     +"have_special_relative": 0,
   }

如何計算larevel中兩個日期之間的日期不同

$date1 = new DateTime("2018-01-10 00:00:00");
enter code here`$date2 = new DateTime("2019-05-18 01:23:45");
$difference = $date1->diff($date2);
$diffInSeconds = $difference->s; //45
$diffInMinutes = $difference->i; //23
$diffInHours   = $difference->h; //8
$diffInDays    = $difference->d; //21
$diffInMonths  = $difference->m; //4
$diffInYears   = $difference->y; //1

暫無
暫無

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

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