Reputation: 17
I am having a bit of trouble working out how to validate whether a timezone has passed a certain time (local to the time zone).
So for instance, if the time in London has passed 18:00:00
$tz = new DateTimeZone('Europe/London');
$datetime1->setTimezone($tz); // calculates with new TZ now
if ($datetime1->format('H:i:s') >= strtotime('18:00:00')) {
echo "time has passed";
} else {
echo "time has NOT passed";
}
The problem with this is that strtotime('18:00:00') seems to be using the server time. If I echo strtotime('18:00:00'); will return 1470247200 which is the amount of seconds since 1970 but this will not be the 6pm time for another timezone for instance America/New_York which at the time of writing this has not passed.
Any idea how this can be done?
Thanks,
Upvotes: 0
Views: 333
Reputation: 15364
Use DateTime
's own comparison feature since it includes time zone support:
$tz = new DateTimeZone('Europe/London');
$datetime1->setTimezone($tz); // calculates with new TZ now
$datetime2 = new \DateTime('18:00:00', $tz);
if ($datetime1 >= sdatetime2) {
echo "time has passed";
} else {
echo "time has NOT passed";
}
Upvotes: 1
Reputation: 60143
I think this should work:
if ($datetime1->format('H:i:s') >= '18:00:00') {
The left side is a string, and every component contains leading zeros, so you can just do a string comparison with the right side.
(This assumes that you consider midnight of the next day to not have "passed" 18:00:00.)
Upvotes: 0