duanmeng3126 2017-05-13 11:41
浏览 24

碳:显示小时和分钟?

I have recently been using Carbon to display humanized time strings, but for some reason I can only get it to show the main factor, for example, I have a date that it needs to show how long until that date is. So for example, if its 6 days, 4 hours and 32 minutes away from now, it currently only displays '6 days'.

How would I go about getting it to display the hours too, and possibly the minutes? It's kind of horrible looking when it only gives you the days, and many people may want to know more like the hours and seconds?

I can't find anything on the Carbon documentation for this. I am using it inside a laravel 5.3 view if that's even relevant.

Heres my code:

{{ \Carbon\Carbon::createFromTimeStamp(strtotime($item->created_at))->diffForHumans() }}
  • 写回答

1条回答 默认 最新

  • dongmaomou4117 2017-05-13 12:30
    关注

    You shouldn't use diffForHumans()in this case, because it returns only a string on which one can no longer work. This is an end result.

    It is better to work with a Carbon object, like this :

    $created = \Carbon\Carbon::createFromTimeStamp(strtotime($item->created_at));
    

    And then, add this in the view :

    {{ $created ->diff(\Carbon\Carbon::now())->format('%d days, %h hours and %i minutes'); }}
    

    You can extend for a larger period :

    $created ->diff(\Carbon\Carbon::now())->format('%y year, %m months, %d days, %h hours and %i minutes');
    

    EDIT (according to the comments) :

    If you do :

    $diff = $created->diff(Carbon::now());
    var_dump($diff);
    

    You get this result :

    object(DateInterval)[113]
      public 'y' => int 0
      public 'm' => int 0
      public 'd' => int 6
      public 'h' => int 4
      public 'i' => int 32
      public 's' => int 29
      public 'f' => float 0.397424
      public 'weekday' => int 0
      public 'weekday_behavior' => int 0
      public 'first_last_day_of' => int 0
      public 'invert' => int 0
      public 'days' => int 6
      public 'special_type' => int 0
      public 'special_amount' => int 0
      public 'have_weekday_relative' => int 0
      public 'have_special_relative' => int 0
      // results depend of the current time
    

    From there, you browse the elements to create the best answer to your needs.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b