How to compare two dates
Short answer
Date objects are compared with the usual operators:
<?php
$deadline = new DateTimeImmutable('2025-06-01');
$now = new DateTimeImmutable('2025-05-20');
var_dump($now < $deadline);Output:
bool(true)Strings
Dates in the YYYY-MM-DD format can be compared as strings — the order matches the calendar:
<?php
var_dump('2025-05-20' < '2025-06-01');Output:
bool(true)What not to do
Strings in the DD.MM.YYYY format cannot be compared this way: the comparison goes by the first characters, that is by the day:
<?php
var_dump('20.05.2025' < '01.06.2025');Output:
bool(false)Functions used in this recipe
- DateTimeImmutable — A date and time object that cannot be changed: every operation returns a new object.
- strtotime — Turns a textual date description into a timestamp.