Last active
November 27, 2023 16:25
-
-
Save MrPunyapal/641eadd4c0ae42d01eeab99f4df32699 to your computer and use it in GitHub Desktop.
Temporal Tricks with Carbon: A PHP Gist Featuring hasTestNow() and setTestNow()
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
<?php | |
// π Birthday Check Function π | |
use Carbon\Carbon; | |
function checkBirthday($birthday) | |
{ | |
return Carbon::now()->isSameDay($birthday) | |
? "ππ₯³ Happy Birthday! π Best wishes for an amazing year ahead! π" | |
: "π No birthday today. Make it an awesome day anyway! π"; | |
} | |
// Example: Without TestNow | |
echo checkBirthday(Carbon::create(1990, 11, 23)) . PHP_EOL; | |
// π No birthday today. Make it an awesome day anyway! π | |
// Example: With TestNow | |
Carbon::setTestNow(Carbon::create(2023, 11, 23)); | |
echo checkBirthday(Carbon::create(1990, 11, 23)) . PHP_EOL; | |
// ππ₯³ Happy Birthday! π Best wishes for an amazing year ahead! π | |
// Reset to real-time β° | |
Carbon::setTestNow(null); | |
// Ready to roll? Check if the test time is set! π¦ | |
if (Carbon::hasTestNow()) { | |
// π¨ Test environment active! Handle accordingly... π¨ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment