V
V
vladislav9972021-06-15 09:44:35
PHP
vladislav997, 2021-06-15 09:44:35

How can I correct the date in this case?

Such a question, there is a date, for example 2020-06-15. You need to add one month a year ahead, and also change the number to the last day of the month. That is, in the end it should turn out:
2020-06-15 (first date)
2020-07-31 (31 days in July)
2020-08-31 (31 days in August)
2020-09-30 (30 days in September)
2020 -10-31 (31 days in October)
and so on until July 22.

I try like this:

//
            foreach ($getDates as $getDate) {
                # предположим что в getDate лежит - 2020-06-15
                $firstDate = new \DateTime($getDate);
                $datesArr[] = $firstDate->format('Y-m-d');
                    for ($i = 1; $i <= 12; $i++) {
                        $date = new \DateTime($getDate);

                        $month = $date->format('F');
                        $year = $date->format('Y');

                        $date->modify("+$i month");
                        # до этого момента все хорошо, прибавляется по одному мес на год вперед
                        $date->modify("last day of $month $year");
                        # здесь уже все ломается, хотя в доке указано "last day of July 2008"
                        $datesArr[] = $date->format('Y-m-d');
                    }
            }

but it doesn't work quite right. after the line modify("last day of $month $year"); tell me how to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question