Created
December 31, 2019 17:20
-
-
Save JacobBennett/66eb73132c732dfbc1a13c814497f9ab to your computer and use it in GitHub Desktop.
A little mess I had on my hands today
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 | |
$start = Carbon::parse('2019-12-24'); | |
$end = Carbon::parse('2020-01-04'); | |
$birthDate = Carbon::parse('1985-12-24')->addDays(random_int(0,6)); | |
// figure out if month and day of birthDate is between the $start and $end |
@midnite81 @ahinkle and @phppirate it looks like you guys all arrived at a pretty similar solution. This works great actually! Smart thinking. I went a little bit different route which was to get a format('md')
of all the dates I wanted to check, and then see if the format('md')
of the date I want to check is contained in that collection.
Here is the pseudo code version.
$start = today()->startOfWeek();
$dates = collect(range(0, 6))->map(function ($days) use ($start) {
return $start->clone()->addDays($days)->format('md');
});
$dates->contains($birthday->format('md'));
I'm storing that set of $dates
on the object when iterating over the users to check if anyone has a birthday, so I don't have to generate the range every time. Works well.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First Attempt at this
Note: This works for birthdays in week and new year overlap but does not handle if your "between dates" being in more than 2 years.