: 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.