Recently made a settlement system, need to be automatically settled at the end of the month, then the first to determine whether the day is not the last day of one months, the algorithm is very simple, posted out as a note of their own, this article is mainly to share with you how to judge how many days in a certain year, hope to help everyone.
/** * Determine how many days a month a year * @return [type] [description] */function daysinmonth ($year = ", $month =") {if (empty ($year)) $year = Date (' Y '), if (Empty ($month)) $month = Date (' m '); if (In_array ($month, Array (1, 3, 5, 7, 8, ' 01 ', ' 03 ', ' 05 ', ' 07 ', ' 08 ', 10, 12) ) { $text = ' + '; }elseif ($month = = 2 | | $month = = ' ") { if ($year% 400 = = 0) | | (($year% 4 = = 0) && ($year% 100!== 0))) { //Judging whether it is a leap year $text = ' + '; Leap Year February} else { $text = ' + '; Common year February} } else { $text = ' + '; Month small}return $text;}
The top is pure arithmetic, and of course there's a simpler way:
/** * determine how many days a certain month of a year * @return [type] [description] * * Function daysInmonth1 ($year = ", $month =") {if (empty ($year)) $year = Date (' Y '); if (empty ($month)) $month = Date (' m '), $day = ' 01 ';//whether the detection date is valid if (!checkdate ($month, $day, $year)) return ' entered in the wrong time ';// Gets the timestamp of the first day of the current month (hour, minute, second, month, day, year) $timestamp = Mktime (0,0,0, $month, $day, $year); $result = date (' t ', $timestamp); return $ result;}