Skip to content

Instantly share code, notes, and snippets.

@MrPunyapal
Last active November 27, 2023 16:25
Show Gist options
  • Save MrPunyapal/641eadd4c0ae42d01eeab99f4df32699 to your computer and use it in GitHub Desktop.
Save MrPunyapal/641eadd4c0ae42d01eeab99f4df32699 to your computer and use it in GitHub Desktop.
Temporal Tricks with Carbon: A PHP Gist Featuring hasTestNow() and setTestNow()
<?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