Home > Backend Development > PHP Tutorial > An interesting date logic processing in php_PHP tutorial

An interesting date logic processing in php_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:19:42
Original
973 people have browsed it

Dealt with a very small problem 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 this way.

Copy 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 with this.
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 with this processing, just as we expected.
If the date is 2011-07-24, we expect $start_date to be 2011-07-11, but what is actually returned is 2011-07-18.

As a last resort, I changed the method
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"));

This is OK.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325208.htmlTechArticleA small problem was solved today. The demand is such that from Monday to Sunday only the data from last Monday to last Sunday can be seen. Here, query the range directly from the database based on the date field...
Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template