PHP

HOW TO COMPARE DATES IN PHP

(simple examples)

PARSE INTO DATE-TIME OBJECTS $dtA = new DateTime("2019-08-07"); $dtB = new DateTime("2017-08-09");

DATE TIME OBJECT

01

COMPARE DATES if ($dtA > $dtB) { echo "A is later"; } else if ($dtA < $dtB) { echo "B is later"; } else { echo "AB are the same"; }

PARSE INTO UNIX TIMESTAMPS $dtA = strtotime("2019-08-07"); $dtB = strtotime("2017-08-09");

UNIX TIMESTAMP

02

COMPARE DATES if ($dtA > $dtB) { echo "A is later"; } else if ($dtA < $dtB) { echo "B is later"; } else { echo "AB are the same"; }

DATE MUST BE IN YYYY-MM-DD $dtA = "2019-08-07"; $dtB = "2019-10-11";

DATE STRING COMPARE

03

COMPARE DATES if ($dtA > $dtB) { echo "A is later"; } else if ($dtA < $dtB) { echo "B is later"; } else { echo "AB are the same"; }