views:

40

answers:

2

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 switch $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.

+1  A: 

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.

Gavin Gilmour
+1. didn't know you can do the datestring and the modifier in one go. Changed your single quotes to double quotes though, because with single quotes $datestring wont be evaluated.
Gordon
Right enough, I'm not sure if there a proper preferred method myself. Cheers for spotting the quotes.
Gavin Gilmour
Okay, that did it. The double quotes makes it work. However I think what was tripping me up was the comma between '$dateString, -6 days'.
Bryan
+1  A: 

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.

Gordon