dtu11716
2018-10-19 02:08 阅读 101
已采纳

Laravel:显示刀片中两个日期之间的差异

I want to know if it is possible to show in a blade view the difference between the system date and a record of the database with date format ($ticket->start_date) without using Carbon.

For example (I know, it is not working)

<td>{{ diff(date('Y-m-d'), strtotime($ticket->start_date))}}</td>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dpmrakfbx820320638 dpmrakfbx820320638 2018-10-19 02:24

    You've got two separate problems here:

    First: how do you diff two dates. You can go high-tech or low-tech here. If you don't want to use Carbon, I suggest going low-tech:

    <?php
    // Note, this gives you a timestamp, i.e. seconds since the Epoch.
    $ticketTime = strtotime($ticket->start_date);
    
    // This difference is in seconds.
    $difference = $ticketTime - time();
    

    At this point, you've got to decide how you want to output the difference. In seconds? In hours?

    Difference: {{ $difference }} seconds
    
    Difference: {{ round($difference / 3600) }} hours
    
    Difference: {{ round($difference / 86400) }} days
    

    You'll have to do extra engineering if you want something as pretty as Carbon::diffForHumans().

    Second: This now becomes a question for you whether this is too much code for your front-end. Obviously you could reduce all of the above to a one-liner in PHP, but your co-workers may not appreciate how unreadable it becomes:

    {{ round((strtotime($ticket->start_date) - time()) / 3600) }} hours
    

    Caveats

    Using timestamps ducks the issue of dealing with timezones. For a bunch of use cases, this is sufficient; for others this is woefully inadequate. But if you want to deal with timezones, you're definitely better off using Carbon, which is better than using PHP's DateTime, but up to you.

    点赞 评论 复制链接分享
  • doulan8054 doulan8054 2018-10-19 02:14

    function

    date_diff()

    Parameters

    There are Two paramers.
    You need to pass DateTmime objects to those parameters.

    Returns

    This function return a DateInterval object.
    You can format DateInterval object using format() function.

    Example

    <td>{{ date_diff(new \DateTime($ticket->start_date), new \DateTime())->format("%m Months, %d days"); }}</td>
    

    you can type anything in this format() string.

    this example format returns something like "9 Months, 18 days"

    DateInterval Object

    this is an example of DateInterval object.
    you can access these properties via format() function posted as below.

      interval: + 9m 18d 02:27:21.488037
      +"y": 0
      +"m": 9
      +"d": 18
      +"h": 2
      +"i": 27
      +"s": 21
      +"f": 0.488037
      +"weekday": 0
      +"weekday_behavior": 0
      +"first_last_day_of": 0
      +"invert": 0
      +"days": 291
      +"special_type": 0
      +"special_amount": 0
      +"have_weekday_relative": 0
      +"have_special_relative": 0
    
    点赞 评论 复制链接分享
  • dongli4711 dongli4711 2018-10-19 11:04

    Please use Carbon lib which is part of laravel. https://carbon.nesbot.com/docs/#api-difference

    dd( \Carbon\Carbon::now()->diff( \Carbon\Carbon::parse('your db datetime') ) );
    

    Example:

    dd( \Carbon\Carbon::now()->diff( \Carbon\Carbon::parse('2019-12-12 00:00:00') ) );
    
    点赞 评论 复制链接分享

相关推荐