How can I compare two dates in PHP?

前端 未结 13 2312
一整个雨季
一整个雨季 2020-11-22 06:02

How can I compare two dates in PHP?

The date is stored in the database in the following format

2011-10-2

If I wanted to

13条回答
  •  借酒劲吻你
    2020-11-22 06:54

    If you want a date ($date) to get expired in some interval for example a token expiration date when performing a password reset, here's how you can do:

    $date = $row->expireDate;
    
    $date->add(new DateInterval('PT24H')); // adds 24 hours
    
    $now = new \DateTime();
    
    if($now < $date) { /* expired after 24 hours */ }
    

    But in your case you could do the comparison just as the following:

    $today = new DateTime('Y-m-d');
    
    $date = $row->expireDate;
    
    if($today < $date) { /* do something */ }
    

提交回复
热议问题