![](/img/trans.png)
[英]PHP Calculate Exact number of hours minutes and seconds between two timestamps
[英]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,
}
$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.