I can’t get the last day of the month. What’s the mistake?

Asked

Viewed 29 times

-1

Because this code does not show the last day of the month?

    $whiteDays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Saturday", "Sunday", "Friday"];
    $year = 2020;
    $month = 4;
    $from = new DateTimeImmutable("$year-$month");
    $to = $from->modify('last day of this month');
    $datePeriod = new DatePeriod($from, DateInterval::createFromDateString('1 day'), $to);

    $dates = iterator_to_array($datePeriod);

    $filtered = array_filter($dates, function (DateTimeImmutable $day) use ($whiteDays) {

        if (in_array($day->format('l'), $whiteDays)) {
            return true;
        }
    });

I need to return all the coded dates to valid $whiteDays. When I run, I have a list but never the last day of the month appears, which can be?

1 answer

0

Dateperiod excludes the end of the interval (in this case, $to). If you want to include the last day of the month, make $to the same as the first day of the next month.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.