php - 在 Laravel 中减去时间

标签 php laravel time

我有两个字段 Mysql 数据库中的开始时间和结束时间。 我想计算 Laravel View 内的持续时间。 所以我写道:

{{ $date1=date('G:i',strtotime($timeSlot->beginningTime)) }}
{{ $date2=date('G:i',strtotime($timeSlot->endingTime)) }}
{{ $date2-$date1 }}

我的问题是: 差异只给我几个小时的差异,没有分钟的差异......

感谢您的帮助

最佳答案

您可以使用 Carbon 来实现这一点。刚刚测试,完美运行:

$difference = Carbon::parse($timeSlot->beginningTime)->diff(Carbon::parse($timeSlot->endingTime));

在 View 中:

{{ $difference->h }}:{{ $difference->i }}

如果您cast dates ,你甚至不需要解析:

$difference = $timeSlot->beginningTime->diff($timeSlot->endingTime);

关于php - 在 Laravel 中减去时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48417722/

相关文章:

c# - 我可以强制 ASP.Net 网站显示特定时区的时间吗?

php - 用里面的php代码刷新Div?

php - 时间字符串到时间戳

php - 如何使用查询从数组中的 id 中获取名称?

php - 如何使用 phpmailer 在带有 html 的邮件中正确添加图像?

php - Raspberry Pi 是否足够强大来运行 Laravel 应用程序?

php - laravel 4 在哪里存储选择字段值?数据库还是数组?

php - 我尝试播种我的播种文件,但收到此错误

带有 time() 的 Python 时间戳

javascript - 对象属性在单独打印后看起来有所不同