In php, get the number of days of the month for the specified date

Source: Internet
Author: User
: This article describes how to obtain the number of days of the month in php. For more information about PHP tutorials, see. At work, I often encounter the problem of getting the number of days in a month. I previously solved this problem.

// This example uses June 1 as an example. $ inputs ['month'] = '20160301'; // The passed parameter $ tmp_month = $ inputs ['month']. "01"; // $ tmp_month = '20140901'; $ s_time = strtotime ($ tmp_month); // month start time $ e_time = strtotime (date ("Ymd ", $ s_time ). '+ 1 month'); // The end time of the month $ countMins = ($ e_time-$ s_time)/60; // The number of minutes in the current month $ countDay = $ countMins/(60*24); // The number of days in the current month
But now, we have found a better way to get it.

Php built-in function date (), you can directly obtain the number of days of the month.

$ Tmp_month = $ inputs ['month']. "01"; $ countDay = date ("t", strtotime ($ tmp_month); // If date ("t") is used directly, the number of days in the current month is obtained.

So easy!

In fact, there is another way to use the cal_days_in_month () function. The php Manual explains: return the number of days of a month in a calendar.

Three parameters:
Calendar used for computing
Month indicates a month in the selected calendar.
Year indicates the year in the selected calendar.

$num = cal_days_in_month(CAL_GREGORIAN, 11, 2015); // 30

I personally prefer the date () function, so I don't need to consider calendar issues.

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The preceding section describes how to obtain the number of days of a specified date in php, including the number of days of the current month. I hope to help anyone who is interested in the PHP Tutorial.

    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.