Reputation: 10922
I'm trying to compare 2 dates, by PHP's documentation :
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 > $date2); //false
So now is not bigger than tomorrow, so it's false. Now I have this :
$date_start = \DateTime::createFromFormat('d/m/Y H:i', '18/07/2018 16:20');
$date_start_format = $date_start->format('d/m/Y H:i');
$date_end = \DateTime::createFromFormat('d/m/Y H:i', '01/08/2018 21:45');
date_end_format = $date_end->format('d/m/Y H:i');
var_dump($date_start_format > $date_end_format); //True
Now when I try to compare an ancient date, with tomorrow, like in the example, I get true. Which should return false, can someone please explain why?
Upvotes: 1
Views: 232
Reputation: 7609
You should compare DateTime
objects:
var_dump($date_start > $date_end);
# bool(false)
By comparing the result of the format()
method you are comparing strings which is not what you are expecting to do.
To understand, compare your formatted dates character by character:
18/07/2018 16:20
01/08/2018 21:45
^-- 1 > 0
Upvotes: 7
Reputation: 1246
Try calculating the time stamp of each date then compare them
var_dump($date1->getTimestamp() > $date2->getTimestamp())
Upvotes: 0