Finding the number of days between two dates

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

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

30条回答
  •  渐次进展
    2020-11-22 00:27

    From PHP Version 5.3 and up, new date/time functions have been added to get difference:

    $datetime1 = new DateTime("2010-06-20");
    
    $datetime2 = new DateTime("2011-06-22");
    
    $difference = $datetime1->diff($datetime2);
    
    echo 'Difference: '.$difference->y.' years, ' 
                       .$difference->m.' months, ' 
                       .$difference->d.' days';
    
    print_r($difference);
    

    Result as below:

    Difference: 1 years, 0 months, 2 days
    
    DateInterval Object
    (
        [y] => 1
        [m] => 0
        [d] => 2
        [h] => 0
        [i] => 0
        [s] => 0
        [invert] => 0
        [days] => 367
    )
    

    Hope it helps !

提交回复
热议问题