How do I compare two DateTime objects in PHP 5.2.8?

后端 未结 7 1969
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 17:42

Having a look on the PHP documentation, the following two methods of the DateTime object would both seem to solve my problem:

  • DateTime::diff : Get
7条回答
  •  失恋的感觉
    2020-11-22 17:57

    As of PHP 7.x, you can use the following:

    $aDate = new \DateTime('@'.(time()));
    $bDate = new \DateTime('@'.(time() - 3600));
    
    $aDate <=> $bDate; // => 1, `$aDate` is newer than `$bDate`
    

提交回复
热议问题