Finding the number of days between two dates

后端 未结 30 2510
心在旅途
心在旅途 2020-11-21 23:47

How to find number of days between two dates using PHP?

相关标签:
30条回答
  • 2020-11-22 00:14

    Try using Carbon

    $d1 = \Carbon\Carbon::now()->subDays(92);
    $d2 = \Carbon\Carbon::now()->subDays(10);
    $days_btw = $d1->diffInDays($d2);
    

    Also you can use

    \Carbon\Carbon::parse('')
    

    to create an object of Carbon date using given timestamp string.

    0 讨论(0)
  • 2020-11-22 00:15

    Convert your dates to unix timestamps, then substract one from the another. That will give you the difference in seconds, which you divide by 86400 (amount of seconds in a day) to give you an approximate amount of days in that range.

    If your dates are in format 25.1.2010, 01/25/2010 or 2010-01-25, you can use the strtotime function:

    $start = strtotime('2010-01-25');
    $end = strtotime('2010-02-20');
    
    $days_between = ceil(abs($end - $start) / 86400);
    

    Using ceil rounds the amount of days up to the next full day. Use floor instead if you want to get the amount of full days between those two dates.

    If your dates are already in unix timestamp format, you can skip the converting and just do the $days_between part. For more exotic date formats, you might have to do some custom parsing to get it right.

    0 讨论(0)
  • 2020-11-22 00:15
    $start = '2013-09-08';
    $end = '2013-09-15';
    $diff = (strtotime($end)- strtotime($start))/24/3600; 
    echo $diff;
    
    0 讨论(0)
  • 2020-11-22 00:16

    Used this :)

    $days = (strtotime($endDate) - strtotime($startDate)) / (60 * 60 * 24);
    print $days;
    

    Now it works

    0 讨论(0)
  • 2020-11-22 00:16

    Easiest way to find the days difference between two dates

    $date1 = strtotime("2019-05-25"); 
    $date2 = strtotime("2010-06-23");
    
    $date_difference = $date2 - $date1;
    
    $result =  round( $date_difference / (60 * 60 * 24) );
    
    echo $result;
    
    0 讨论(0)
  • 2020-11-22 00:17
    $now = time(); // or your date as well
    $your_date = strtotime("2010-01-31");
    $datediff = $now - $your_date;
    
    echo round($datediff / (60 * 60 * 24));
    
    0 讨论(0)
提交回复
热议问题