我想显示created_at
与当前日期和时间之间的小时数和分钟数差异。
目前,我有
{{ $calls['created_at']->hour }} hrs, {{ $calls['created_at']->minute}} mins
我该如何实现
答案 0 :(得分:2)
尝试一下,
$now = Carbon::now();
$created_at = Carbon::parse($calls['created_at']);
$diffHuman = $created_at->diffForHumans($now); // 3 Months ago
$diffHours = $created_at->diffInHours($now); // 3
$diffMinutes = $created_at->diffInMinutes($now) // 180
答案 1 :(得分:1)
要精确地获得现在使用碳获得的结果,请使用->diffForHumans
作为
use Carbon\Carbon;
$diff = $calls['created_at']->diffForHumans(null, true, true, 2);
echo str_replace(['h', 'm'], ['hrs', 'mins'], $diff);
已更新:根据您的评论,我认为
$calls['created_at']
是Carbon
对象
答案 2 :(得分:0)
尝试{{ $calls->created_at->diffForHumans() }}
答案 3 :(得分:0)
您可以使用carbon类将其转换为秒,然后将其转换为小时
$seconds = Carbon::diffInSeconds($calls['created_at']);
gmdate("H:i:s", $seconds);