开发者

Trying to find the previous six days given any date

开发者 https://www.devze.com 2022-12-24 00:20 出处:网络
I\'m using strtotime just fine for finding the previous week and next week\'s entries in my database, but what I can\'t seem to find is how to find the previous six days if the user selects a past dat

I'm using strtotime just fine for finding the previous week and next week's entries in my database, but what I can't seem to find is how to find the previous six days if the user selects a past date.

Here's how I know what today and six days previous are:

$today = date("Y-m-d");
$minus6 = date('Y-m-d', strtotime('-6 days'));

Now how can I s开发者_StackOverflowwitch $today with $dateString as provided by my users' input? I thought something like this based on my google searches, but it yields no results:

$dateString = 2010-01-25; // for example
$minus6 = date('Y-m-d', strtotime('-6 days, $dateString');

Am I missing some fundamental information regarding dates, strtotime, or what? Thanks for any help.


The second param to strtotime is a timestamp from which the first argument will be calculated:

echo date('Y-m-d', strtotime('-6 days', strtotime($dateString));

But you can also do it like Gavin suggested.


You should put the actual date before any of the modifiers to strtotime(). For example:

$dateString = 2010-01-25; // for example
$minus6 = date('Y-m-d', strtotime('-6 days, $dateString'));

Should become:-

$dateString = "2010-01-25"; // for example
$minus6 = date('Y-m-d', strtotime("$dateString -6 days"));

...or pass it in as an explicit timestamp as a second parameter to strtotime() as per Gordon's answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号