Link to home
Start Free TrialLog in
Avatar of Daniel Wilson
Daniel WilsonFlag for United States of America

asked on

PHP 5.2 -- Today's date - 7 days

How do I say:
$StartDate = Today's Date - 7 Days
in PHP 5.2?

I've seen stuff where the date is decomposed into month, day, year, subtracted and re-assembled.  But that looks like August 5th - 7 days would come out to August -2nd ... which isn't really valid.

I was trying the DateTime::sub routine, but then found that my host is running 5.2, not 5.3.

I know this is a common need ... how is it done?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Daniel Wilson

ASKER

gr8gonzo, your code snippet worked great (once I corrected a little misspelling).  Thanks!
07/14/2009

angelIII, yours gives me something funny ... but thanks for the shot at it!
1247582822

Ah - sorry. My laptop keyboard at home sometimes repeats characters that I type.

angelll's suggestion technically works. It simply returned a unix timestamp. The strtotime() function takes a formatted date or English description of a date and changes it into a timestamp:

$januaryFirst2009Timestamp = strtotime("01/01/2009");
print date("m/d/Y",$januaryFirst2009Timestamp);

That code basically converts a date into a timestamp and then converts it back to the date again. Not really a useful piece of code, but it's a good illustration of converting back-and-forth between dates and timestamps.

Try date-formatting angelll's code:
echo date("m/d/Y",strtotime("-1 week"));