How PHP determines how many days a certain month of a year

Source: Internet
Author: User
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;} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.