Home » PHP » PHP gets the number of days of the month and the f...

PHP gets the number of days of the month and the first day of the month and the last day, the first day of the last month


In the query process, for example to check last month from the first day to the last day of the Commission (Commission), the US in the course of programming is to let the program range query in last month, the first day is relatively easy to handle, but at the end of the day is not fixed, to write a paragraph the function of month and year of judgment came last month the total number of days. It is in trouble, and get the current month of the current year, the conventional date acquisition function, the following code is correct by the engineers after the test code, you can rest assured that the use.

1. get the first and last day of last month.

Echo, date ('Y-m-01', strtotime ('-1, month'));

Echo "
";

Echo, date ('Y-m-t', strtotime ('-1, month'));

Echo "
";

2. get the first and last day of the month.

$BeginDate=date ('Y-m-01', strtotime (date ("Y-m-d"));

Echo $BeginDate;

Echo "
";

Echo date ('Y-m-d', strtotime ($BeginDate, +1, month, day, -1))

Echo "
";

3. get the day, month, day and number of days.

Echo, "this month there are:".Date ("t"). "Heaven";

Echo current year.Date ('Y');

Echo current month.Date ('m');

Echo "current number".Date ('d');

Echo "
";

4. use function and array to get the first and last day of the month, more practical, from friends.

Function getthemonth ($date)

{

$firstday = date ('Y-m-01', strtotime ($date));

$lastday = date ('Y-m-d', strtotime ($firstday, +1, month, day, -1))

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 "
";




Latest