I'm trying to populate an array with every particular weekday, say Sunday, between a start date and an end date.
The best I could think of is as below. (The dates are actually Carbon dates.)
This seems to work, except that I have noticed when I dd()
the result, all array elements are assigned the final value of $d
(= $endDate_Planned
). If I info()
log these I see that its working at the point in time each is pushed into the array, but obviously the values are some sort of reference that gets updated every time $d
updates/increments.
Is there a way to avoid that?
for( $d = $startDate_Planned; $d <= $endDate_Planned; $d->addDays(7) ) {
info($d);
$dateArray_Planned[] = $d;
}
My Answer:
= $endDate_Planned; $d->addDay()){
if($d->dayOfWeek == Carbon::SUNDAY){
$weekdays[] = $d->copy();
}
}
Rate this post
3 of 5 based on 9895 votesComments