Finding the number of days between two dates

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

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

相关标签:
30条回答
  • 2020-11-22 00:07
        // Change this to the day in the future
    $day = 15;
    
    // Change this to the month in the future
    $month = 11;
    
    // Change this to the year in the future
    $year = 2012;
    
    // $days is the number of days between now and the date in the future
    $days = (int)((mktime (0,0,0,$month,$day,$year) - time(void))/86400);
    
    echo "There are $days days until $day/$month/$year";
    
    0 讨论(0)
  • 2020-11-22 00:08
    $datediff = floor(strtotime($date1)/(60*60*24)) - floor(strtotime($date2)/(60*60*24));
    

    and, if needed:

    $datediff=abs($datediff);
    
    0 讨论(0)
  • 2020-11-22 00:10

    Using this simple function. Declare function

    <?php
    function dateDiff($firstDate,$secondDate){
        $firstDate = strtotime($firstDate);
        $secondDate = strtotime($secondDate);
    
        $datediff = $firstDate - $secondDate;
        $output = round($datediff / (60 * 60 * 24));
        return $output;
    }
    ?>
    

    and call this function like this where you want

    <?php
        echo dateDiff("2018-01-01","2018-12-31");    
    
    // OR
    
        $firstDate = "2018-01-01";
        $secondDate = "2018-01-01";
        echo dateDiff($firstDate,$secondDate);    
    ?>
    
    0 讨论(0)
  • 2020-11-22 00:12

    Calculate the difference between two dates:

    $date1=date_create("2013-03-15");
    $date2=date_create("2013-12-12");
    
    $diff=date_diff($date1,$date2);
    
    echo $diff->format("%R%a days");
    

    Output: +272 days

    The date_diff() function returns the difference between two DateTime objects.

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

    If you are using MySql

    function daysSince($date, $date2){
    $q = "SELECT DATEDIFF('$date','$date2') AS days;";
    $result = execQ($q);
    $row = mysql_fetch_array($result,MYSQL_BOTH);
    return ($row[0]);
    

    }

    function execQ($q){
    $result = mysql_query( $q);
    if(!$result){echo ('Database error execQ' . mysql_error());echo $q;}    
    return $result;
    

    }

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

    Looking all answers I compose universal function what working on all PHP versions.

    if(!function_exists('date_between')) :
        function date_between($date_start, $date_end)
        {
            if(!$date_start || !$date_end) return 0;
    
            if( class_exists('DateTime') )
            {
                $date_start = new DateTime( $date_start );
                $date_end   = new DateTime( $date_end );
                return $date_end->diff($date_start)->format('%a');
            }
            else
            {           
                return abs( round( ( strtotime($date_start) - strtotime($date_end) ) / 86400 ) );
            }
        }
    endif;
    

    In the general, I use 'DateTime' to find days between 2 dates. But if in the some reason, some server setup not have 'DateTime' enabled, it will use simple (but not safe) calculation with 'strtotime()'.

    0 讨论(0)
提交回复
热议问题