When we use PHP to get the last MONTH, we usually use strtotime (-1 MONTH), but sometimes this will be inaccurate, such as today is, strtotime (-1 MONTH) the result is February 2013, instead of the expected date of January 1,. The preliminary assumption is that this method is to first find the number of days in the previous month, and then use today
When we use PHP to get the last MONTH, we usually use strtotime ('-1 month'), but sometimes this will be inaccurate, for example, today is, the result of strtotime ('-1 month') is February 2013, instead of the expected date. The preliminary assumption is that this method is to first find the number of days of the previous MONTH, and then use today
When we use PHP to get the last MONTH, we usually use strtotime ('-1 month'), but sometimes this will be inaccurate, for example, today is, the result of strtotime ('-1 month') is February 2013, instead of the expected date. The preliminary assumption is that this method is to first find the number of days in the previous MONTH, then, subtract the number of days in the previous month from the current day...
There are several methods to help us achieve the expected results, for example, I want to return the month of the previous month:
Echo date ('m y', strtotime ('midnight first day of-1 month '));
Or:
Echo date ('m y', strtotime (date ('Y-m-01 ')-86400 );
Below are other purposes:
Strtotime ('first day of last month ');
Strtotime ('Last day of last month ');
Strtotime ('First of last week ');
Strtotime ('First of this week ');
Strtotime ('This week midnight '); // returns Monday midnight of this week
Strtotime ('Last week midnight '); // returns Monday midnight of last week
Strtotime ('Last week Sunday midnight '); // returns Sunday midnight of this week
Strtotime ('-2 weeks Sunday midnight'); // returns Sunday midnight of last week
Date_default_timezone_set ('Asia/Shanghai ');
$ First_day_of_month = date ('Y-m', time (). '- 01 00:00:01 ';
$ T = strtotime ($ first_day_of_month );
Print_r (array (
Date ('Y-mmonth', $ t ),
Date ('Y-mmonth', strtotime ('-1 month', $ t )),
Date ('Y-mmonth', strtotime ('-2 month', $ t )),
));
?>
Original article address: other use of strtotime () PHP is inaccurate last month and next month. Thank you for sharing it with me.