dpxua26604 2017-10-03 04:48
浏览 144
已采纳

如何在laravel 5.1中获得created_at和updated_at的区别

I've researched about on internet but what I found is only getting the past time (e.g. 4mins ago), What I want is to get the difference from created_at and updated_at in my database and like so, 3days 3hrs 23mins 30sec and if there's no mins or hrs, just output the 3days 3hrs 30sec or 3days 23mins 30sec.

What I have:

view

@foreach($submits as $submit)
   {{ $submit->pivot->created_at }} // to get the created_at
   {{ $submit->pivot->updated_at }} // to get the updated_at
@endforeach
  • 写回答

1条回答 默认 最新

  • dtjw6660 2017-10-03 05:06
    关注

    You can find days, hours, minutes and seconds by using this function.

    public function getDifference($created_at, $updated_at) {
        $days = $created_at->diffInDays($updated_at);
        $hours = $created_at->diffInHours($updated_at->subDays($days));
        $minutes = $created_at->diffInMinutes($updated_at->subHours($hours));
        $seconds = $created_at->diffInSeconds($updated_at->subMinutes($minutes));
        return CarbonInterval::days($days)->hours($hours)->minutes($minutes)->seconds($seconds)->forHumans();
    }
    

    Output

    3 days 3 hours 23 minutes 30 seconds
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看