Skip to content

Instantly share code, notes, and snippets.

@joubertredrat
Last active May 1, 2025 20:40
Show Gist options
  • Save joubertredrat/0b5c21148d0cf34a0f85c67424aa815e to your computer and use it in GitHub Desktop.
Save joubertredrat/0b5c21148d0cf34a0f85c67424aa815e to your computer and use it in GitHub Desktop.
<?php
/**
* @author Joubert RedRat
* https://3v4l.org/m7Mp9
*/
function getDateNow(): ?\DateTime {
$datetime = new \DateTime('now');
$minute = (int) $datetime->format('i');
return $minute % 2 === 0 ? $datetime : null;
}
function getUpdatedEarly(): \DateTime {
$d = getDateNow();
if (!$d instanceof \DateTime) {
throw new \Exception('oooops by early');
}
return $d;
}
function getUpdatedEarlyCoalescing(): \DateTime {
$d = getDateNow();
return $d ?? throw new \Exception('oooops by coalescing');
}
try {
$d = getUpdatedEarly();
echo $d->format('Y-m-d H:i:s');
} catch (\Throwable $e) {
echo $e->getMessage();
}
echo PHP_EOL;
try {
$d = getUpdatedEarlyCoalescing();
echo $d->format('Y-m-d H:i:s');
} catch (\Throwable $e) {
echo $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment