Other use of strtotime () PHP is inaccurate last month and next month

Source: Internet
Author: User
Tags echo date
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.

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.