PHP date calculation

前端 未结 11 1687
不知归路
不知归路 2021-01-01 08:19

What is the best (date format independent way) in PHP to calculate difference in days between two dates in specified format.

I tried the following function:

11条回答
  •  既然无缘
    2021-01-01 08:51

    The Zend Framework has the class Zend_Date for dealing with "date math". It works around system specific timestamp limits by using the BCMath extension, or if that's not available limits the timestamps by max float value for your system.

    // example printing difference in days
    require('Zend/Date.php');
    
    $date1 = new Zend_Date();
    $date1->set(2, Zend_Date::MONTH);
    $date1->set(27, Zend_Date::DAY);
    $date1->set(2008, Zend_Date::YEAR);
    
    $date2 = new Zend_Date();
    $date2->set(3, Zend_Date::MONTH);
    $date2->set(3, Zend_Date::DAY);
    $date2->set(2008, Zend_Date::YEAR);
    
    echo ($date2->getTimestamp() - $date1->getTimestamp()) / (3600*24);
    

提交回复
热议问题