Php interesting date logic processing

Source: Internet
Author: User

Today we have handled a very small problem.
This is the requirement. From Monday to Sunday, only data from last Monday to last Sunday can be seen.
In the database tutorial, you can directly query the range based on the date field.
However, PHP needs to generate the start date and end date.

At the very beginning, I did this directly.

$ Start_date = date ('Y-m-D', strtotime ("-2 week Monday "));
$ End_date = date ('Y-m-D', strtotime ("$ start_date + 6 day "));
If the date is, $ start_date =, there is no problem.
If the date is, $ start_date is equivalent to and remains in the last week.

So I changed the method.

$ GetWeekDay = date ("w ");
$ StartDay = date ("Y-m-d", mktime (0, 0, 0, date ("m"), date ("d ") -$ getWeekDay + 1-7, date ("Y ")));
$ EndDay = date ("Y-m-d", strtotime ("+ 6 day $ startDay "));
If the date is, $ start_date =, there is no problem, as we expect.
If the date is, we expect $ start_date to be, but the actual return is 2011-07-18.

I have to change the method.

$ GetWeekDay = date ("N ");
$ StartDay = date ("Y-m-d", mktime (0, 0, 0, date ("m"), date ("d ") -$ getWeekDay + 1-7, date ("Y ")));
$ EndDay = date ("Y-m-d", strtotime ("+ 6 day $ startDay "));
This is OK.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.