Last active
December 19, 2019 21:04
-
-
Save Braunson/0363fac37952e15509b49913741f32b1 to your computer and use it in GitHub Desktop.
Carbon macro for checking if a birthday is between two dates (i.e. is the birthday upcoming within the next 3 months)
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 | |
// Datermines if the birth date given is between two dates. | |
Carbon::macro('isBirthdayBetween', function($start, $end) { | |
// Adjust the birthdate year to the current year | |
$birthday = $this->copy()->year(date('Y')); | |
// If the birthday has past, add a year (nearing the new year) | |
if ($birthday->isPast()) { | |
$birthday = $birthday->addYear(); | |
} | |
return $birthday->between($start, $end); | |
}); | |
// Example Usage | |
$today = Carbon::parse('2019/12/01'); | |
$threeMonths = now()->addMonths(3); | |
$birthday = Carbon::parse('1990/05/24')->isBirthdayBetween($today, $threeMonths); // False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment