How can I compare two dates in PHP?

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

    Here's my spin on how to get the difference in days between two dates with PHP. Note the use of '!' in the format to discard the time part of the dates, thanks to info from DateTime createFromFormat without time.

    $today = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
    $wanted = DateTime::createFromFormat('!d-m-Y', $row["WANTED_DELIVERY_DATE"]);
    $diff = $today->diff($wanted);
    $days = $diff->days;
    if (($diff->invert) != 0) $days = -1 * $days;
    $overdue = (($days < 0) ? true : false);
    print "\n";
    

提交回复
热议问题