I have two Datetimes like this (the dates being actually $vars)
$startTime = \\DateTime::createFromFormat(\'Y/m/d H:i\', \'2015/01/01 23:00\');
$endTime = \\Date
I think this is one of the few situations where the use of strings for date calculations is justified:
function onDifferentDays(\DateTimeInterface $startTime, \DateTimeInterface $endTime){
return $startTime->format('Y-m-d')!==$endTime->format('Y-m-d');
}
This code should be easy to extend to include time zone.
There're other alternatives but I don't think they're normally worth the effort:
Compare element by element (day, month and year):
DateTime
class doesn't offer dedicated functions, only format()
.Normalize both dates to a common time and compare with ==
(not ===
):
Unless you're using immutable objects you need to clone input or expect side effects
You also need to ensure that time exists in the active time zone though midnight is probably safe enough.
Whatever, YMMV ;-)