我有一个名为transactions的表,其中有两个与我的问题有关的字段:_start_timestamp_和_end_timestamp_。我需要对_end_timestamp_不为null的所有事务之间传递的时间求和。因此,结果必须类似于“ 总交易时间:1小时18分钟”
我已经尝试过使用Carbon,但是我不知道如何使用它来汇总表格的所有行。
foreach($timestampStarts as $timestampStart){
$time = new Carbon($timestampStart->start_timestamp);
$shift_end_time =new Carbon($timestampStart->end_timestamp);
dd($time->diffForHumans($shift_end_time));
}
您可以使用MySQL TIMESTAMPDIFF函数来计算差异:
Transaction::whereNotNull('_end_timestamp_')
->sum(DB::raw('TIMESTAMPDIFF(SECOND, _start_timestamp_, _end_timestamp_)'));
这将为您提供总计秒数。