Home >Backend Development >PHP Tutorial >What does it mean to be unfilial and have no descendants? An interesting date logic processing in PHP

What does it mean to be unfilial and have no descendants? An interesting date logic processing in PHP

WBOY
WBOYOriginal
2016-07-29 08:48:23941browse

Dealt with a very small issue today.
The demand is such that from Monday to Sunday, you can only see the data from last Monday to last Sunday.
Here you can query the range directly from the database based on the date field.
But PHP is required to generate the start date and end date.
At first, I handled it like this directly.

Copy the code The code is 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, there is no problem in this way.
If the date is 2011-07-18, $start_date will be equal to 2011-07-04, which is still alive in the last week.
So I changed the method

Copy the code The code is 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, there is no problem in this processing, just as we expected.
If the date is 2011-07-24, we expect $start_date to be 2011-07-11, but the actual returned value is 2011-07-18.
As a last resort, I changed the method again

Copy the code The code is 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" ));


It’s OK now.

The above has introduced an interesting date logic processing in PHP, including the content of what it means to be unfilial and unfilial, and I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn