How can I compare two dates in PHP?

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

    If all your dates are posterior to the 1st of January of 1970, you could use something like:

    $today = date("Y-m-d");
    $expire = $row->expireDate; //from database
    
    $today_time = strtotime($today);
    $expire_time = strtotime($expire);
    
    if ($expire_time < $today_time) { /* do Something */ }
    

    If you are using PHP 5 >= 5.2.0, you could use the DateTime class:

    $today_dt = new DateTime($today);
    $expire_dt = new DateTime($expire);
    
    if ($expire_dt < $today_dt) { /* Do something */ }
    

    Or something along these lines.

提交回复
热议问题