Finding the Last Day of a Month in PHP
Finding the number of days in a month is useful for reporting, billing periods, scheduling, and date validation. PHP can do this directly with its date APIs, including support for leap years. A Simple getLastDay Function The following function accepts a month in YYYY-MM format and returns the number of days in that month: function getLastDay(string $month): int { $date = new DateTimeImmutable($month . '-01'); return (int) $date->format('t'); } How the Code Works Create a date for the first day of the month