Skip to content

Instantly share code, notes, and snippets.

@unreal4u
Last active December 20, 2017 09:48
Show Gist options
  • Save unreal4u/3b1a1fce81408dd3561d5751aa5da7f1 to your computer and use it in GitHub Desktop.
Save unreal4u/3b1a1fce81408dd3561d5751aa5da7f1 to your computer and use it in GitHub Desktop.
Check whether it is already past a certain hour in a timezone
<?php
# 3v4l.org: https://3v4l.org/SpvCY
/**
* Incorrect check: with diff
*/
function isPastDate(\DateTimeImmutable $serverTime, \DateTimeImmutable $localTime): bool
{
$endResult = $serverTime->diff($localTime);
return $endResult->invert === 1;
}
/**
* Incorrect check: a timestamp can be higher in local time than the "server" time
*/
function isPastDateTimestampCheck(\DateTimeImmutable $serverTime, \DateTimeImmutable $localTime): bool
{
return (int)$serverTime->format('U') > (int)$localTime->format('U');
}
/**
* Correct check: the local time is not yet higher than the "server" time
*/
function isPastDateTimeCheck(\DateTimeImmutable $serverTime, \DateTimeImmutable $localTime): bool
{
return (int)$serverTime->format('dmYHis') > (int)$localTime->format('dmYHis');
}
$chicagoTime = new \DateTimeImmutable('today 07:00', new \DateTimeZone('America/Chicago'));
$tomorrowChicago = new \DateTimeImmutable('tomorrow 12:00', new \DateTimeZone('America/Chicago'));
$serverTime = new \DateTimeImmutable('today 10:00', new \DateTimeZone('Europe/Amsterdam'));
var_dump(str_repeat('-', 80));
if (isPastDate($serverTime, $chicagoTime)) {
var_dump('pastDate: it is not yet ' . $serverTime->format('d-m-Y H:i:s') . ' in Chicago ('.$chicagoTime->format('d-m-Y H:i:s').')');
} else {
var_dump('pastDate: it is now ' . $chicagoTime->format('d-m-Y H:i:s') . ' in Chicago, past the local 10:00 limit');
}
if (isPastDateTimestampCheck($serverTime, $chicagoTime)) {
var_dump('pastDateTimestampCheck: it is not yet ' . $serverTime->format('d-m-Y H:i:s') . ' in Chicago ('.$chicagoTime->format('d-m-Y H:i:s').')');
} else {
var_dump('pastDateTimestampCheck: it is now ' . $chicagoTime->format('d-m-Y H:i:s') . ' in Chicago, past the local 10:00 limit');
}
if (isPastDateTimeCheck($serverTime, $chicagoTime)) {
var_dump('pastDateTimeCheck: it is not yet ' . $serverTime->format('d-m-Y H:i:s') . ' in Chicago ('.$chicagoTime->format('d-m-Y H:i:s').')');
} else {
var_dump('pastDateTimeCheck: it is now ' . $chicagoTime->format('d-m-Y H:i:s') . ' in Chicago, past the local 10:00 limit');
}
/* Outputs: */
string(80) "--------------------------------------------------------------------------------"
string(78) "pastDate: it is now 20-12-2017 07:00:00 in Chicago, past the local 10:00 limit"
string(92) "pastDateTimestampCheck: it is now 20-12-2017 07:00:00 in Chicago, past the local 10:00 limit"
string(85) "pastDateTimeCheck: it is not yet 20-12-2017 10:00:00 in Chicago (20-12-2017 07:00:00)"
string(80) "--------------------------------------------------------------------------------"
string(72) "pastDate: it is now 21-12-2017 12:00:00 in Chicago, past the 10:00 limit"
string(92) "pastDateTimestampCheck: it is now 21-12-2017 12:00:00 in Chicago, past the local 10:00 limit"
string(87) "pastDateTimeCheck: it is now 21-12-2017 12:00:00 in Chicago, past the local 10:00 limit"
string(80) "--------------------------------------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment