Filial piety there are three no-no-big what is the meaning of an interesting date in PHP logic processing

Source: Internet
Author: User
A very small problem has been dealt with today.
The requirement is that, from Monday to Sunday, you can only see data from Monday to Sunday.
Here, you can query the range directly from the database based on the Date field.
But you need PHP to generate a start date and an end date.
In the beginning, I dealt with it directly.

Copy the Code code as follows:


$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 2011-07-19, $start _date= 2011-07-11 to deal with it without problems.
If the date is 2011-07-18, the $start _date will be equal to 2011-07-04, still live in the last week.
So I changed the way.

Copy the Code code as follows:


$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 2011-07-19, $start _date= 2011-07-11 This treatment is no problem, as we expected.
If the date is 2011-07-24, we expect the $start _date is 2011-07-11, but the actual return is 2011-07-18.
I have to change the method below.

Copy the Code code as follows:


$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 will be OK.

The above describes the filial piety there are three no longer what is the meaning of an interesting date in PHP logic processing, including the filial piety has three no what the meaning of the content, I hope that the PHP tutorial interested friends have helped.

  • 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.