Php obtains the number of days of the current month, the first and last days of the current month, and the first and last days of the previous month.

Source: Internet
Author: User
: This article describes how to obtain the number of days of the current month, the first and last days of the current month, the first day of the previous month, and the last day of the previous month. if you are interested in the php Tutorial, refer to it. During the query process, for example, to check the Commission (Commission) from the first day of the previous month to the last day of the previous month, we need to make the program query within the range of the previous month during the program implementation process, the first day is easy to handle, but the last day may not be enough. you need to write a segment function to determine the number of days in the last month by month and year. it is more troublesome to get the function of the regular date such as the current month and current year. the following code is the correct code tested by our engineers and can be safely used.
1. obtain the first and last days of the last month.
Echo date ('Y-m-01 ', strtotime ('-1 month '));
Echo"
";
Echo date ('Y-m-t', strtotime ('-1 month '));
Echo"
";
2. obtain the first and last days of the current month.
$ BeginDate = date ('Y-m-01 ', strtotime (date ("Y-m-d ")));
Echo $ BeginDate;
Echo"
";
Echo date ('Y-m-D', strtotime ("$ BeginDate + 1 month-1 day "));
Echo"
";
3. obtain the year, month, day, and day of the current day.
Echo "total for this month:". date ("t"). "day ";
Echo "current year". date ('Y ');
Echo "Current Month". date ('M ');
Echo "current number". date ('D ');
Echo"
";
4. use functions and arrays to obtain the first and last days of the current month. this method is more practical.
Function getthemonth ($ date)
{
$ Firstday = date ('Y-m-01 ', strtotime ($ date ));
$ Lastday = date ('Y-m-D', strtotime ("$ firstday + 1 month-1 day "));
Return array ($ firstday, $ lastday );
}
$ Today = date ("Y-m-d ");
$ Day = getthemonth ($ today );
Echo "the first day of the month:". $ day [0]. "The last day of the month:". $ day [1];
Echo"
";

The above describes how to get php to get the number of days of the current month, the first and last day of the current month, the first day of the previous month, and the last day of the last day, including some content, hope to be helpful to PHP tutorials.

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.