Skip to content

Instantly share code, notes, and snippets.

@Braunson
Last active December 19, 2019 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Braunson/0363fac37952e15509b49913741f32b1 to your computer and use it in GitHub Desktop.
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)
<?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