Last active
September 27, 2019 08:51
-
-
Save gundamew/c3dc348a4dc3278911f9f065be0884e6 to your computer and use it in GitHub Desktop.
Simple helpers to run tasks before/until the deadline.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (! function_exists('stop_before')) { | |
function stop_before(callable $func, DateTimeInterface $dt) | |
{ | |
$tz = $dt->getTimezone(); | |
$now = new DateTime('now', $tz); | |
if ($now > $dt) { | |
call_user_func($func); | |
} | |
return; | |
} | |
} | |
if (! function_exists('run_until')) { | |
function run_until(callable $func, DateTimeInterface $dt) | |
{ | |
$tz = $dt->getTimezone(); | |
$now = new DateTime('now', $tz); | |
if ($now <= $dt) { | |
call_user_func($func); | |
} | |
return; | |
} | |
} | |
// Example | |
$deadline = DateTime::createFromFormat(/* ... */); | |
run_until(function () { | |
// do something | |
}, $deadline); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment