Home > Backend Development > PHP Tutorial > PHP method to obtain the number of days in the current month, the first and last day of the current month, and the first and last day of the previous month

PHP method to obtain the number of days in the current month, the first and last day of the current month, and the first and last day of the previous month

WBOY
Release: 2016-07-30 13:29:48
Original
922 people have browsed it

During the query process, for example, if we want to check the commission (commission) from the first day to the last day of the previous month, then during the program implementation process, we must let the program query within the range of the previous month, the first day It is relatively easy to handle, but the last day is uncertain. You have to write a function to judge the month and year to get the number of days in the last month. That is more troublesome. There are also regular date acquisition functions such as the current month and the current year, as follows The codes are correct after being tested by our company's engineers, and you can use them with confidence.
1. Get the first and last day of the last month.
echo date('Y-m-01', strtotime('-1 month')) ;
echo "
";
echo date('Y-m-t', strtotime('-1 month'));
echo "
";
2. Get the first one of the month day and the last day.
$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. Get the year, month, day and number of days of the day.
echo " This month there are: ".date("t")."Day";
echo "The current year".date('Y');
echo "The current month".date('m');
echo "The current day".date ('d');
echo "
";
4. Use functions and arrays to get the first and last days of the month, which is more practical and comes from netizens.
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: ".$day[1];
echo "
";

The above introduces the implementation method of PHP to obtain the number of days of the current month, the first and last day of the current month, and the first and last day of the previous month, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template