Trying to find the previous six days given any date
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.
精彩评论