Skip to content

Instantly share code, notes, and snippets.

@gundamew
Last active September 27, 2019 08:51
Show Gist options
  • Save gundamew/c3dc348a4dc3278911f9f065be0884e6 to your computer and use it in GitHub Desktop.
Save gundamew/c3dc348a4dc3278911f9f065be0884e6 to your computer and use it in GitHub Desktop.
Simple helpers to run tasks before/until the deadline.
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