One line of code is used to obtain the results of Monday, Sunday, last Monday, last Sunday, 1st day of this month, and this month...

Source: Internet
Author: User
Tags echo date

This Monday
Echo date ('Y-m-d', (Time ()-(date ('W') = 0? 7: Date ('W')-1) * 24*3600); // W indicates the number of weeks. 0 indicates Sunday.

This Sunday
Echo date ('Y-m-d', (Time () + (7-(date ('W') = 0? 7: Date ('W') * 24*3600); // W is also used, which is calculated based on the number of days currently related to Sunday.


Last Monday
Echo date ('Y-m-d', strtotime ('-1 Monday', time (); // no matter the number of today, -1 Monday indicates the last valid Week.


Last Sunday
Echo date ('Y-m-d', strtotime ('-1 Sunday', time (); // The last valid Sunday, also applies to other weeks


1st day of this month
Echo date ('Y-m-d', strtotime (date ('Y-m', time (). '-01 00:00:00'); // generate strtotime directly


Last day of this month
Echo date ('Y-m-d', strtotime (date ('Y-m', time ()). '-'. date ('T', time ()). '00:00:00'); // t indicates the number of days in the current month, 28 to 31 days.


1st day of last month
Echo date ('Y-m-d', strtotime ('-1 month', strtotime (date ('Y-m', time ()). '-01 00:00:00'); // subtract one month from strtotime on January 1, 1st day of this month.


Last day of last month
Echo date ('Y-m-d', strtotime (date ('Y-m', time ()). '-01 00:00:00')-86400); // minus one day on January 1, 1st day of this month is the last day of the previous month.

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.