PHP

HOW TO COMPARE DATES IN PHP

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

DATE TIME OBJECT

01

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

PARSE INTO DATE-TIME OBJECT $dtA = strtotime("2019-08-07"); $dtB = strtotime("2017-08-09");

UNIX TIMESTAMP

02

COMPARE USING OPERATORS 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

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