How can I compare two dates in PHP?

前端 未结 13 2255
一整个雨季
一整个雨季 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:52

    Just to compliment the already given answers, see the following example:

    $today = new DateTime('');
    $expireDate = new DateTime($row->expireDate); //from database
    
    
    
    if($today->format("Y-m-d") < $expireDate->format("Y-m-d")) { 
        //do something; 
    }
    

    Update: Or simple use old-school date() function:

    if(date('Y-m-d') < date('Y-m-d', strtotime($expire_date))){
        //echo not yet expired! 
    }   
    

提交回复
热议问题