Last active
May 1, 2025 20:40
-
-
Save joubertredrat/0b5c21148d0cf34a0f85c67424aa815e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?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