views:

326

answers:

5

I need to calculate the timestamp of exactly 7 days ago using PHP, so if it's currently March 25th at 7:30pm, it would return the timestamp for March 18th at 7:30pm.

Should I just subtract 604800 seconds from the current timestamp, or is there a better method?

+9  A: 
strtotime("-1 week")
SilentGhost
Damn, a whole two seconds slower than you :D
ILMV
@ILMV Next time, don't type "method is your friend" :)
Luís Guilherme
It wasn't that, I split my drink seconds from hitting submit ;)
ILMV
+5  A: 

strotime is your friend

echo strtotime("-1 week");
ILMV
+2  A: 

http://php.net/strtotime

echo strtotime("-1 week");
Aaron W.
+2  A: 
function timestamp_of_exactly_one_week_ago() {
   return time()-7*24*60*60;
}
celalo
This will return 168 hours ago, not necessarily 7 days due to daylight savings.
Mike B
yes you are correct.
celalo
+3  A: 

There is the following example on PHP.net

<?php
  $nextWeek = time() + (7 * 24 * 60 * 60);
               // 7 days; 24 hours; 60 mins; 60secs
  echo 'Now:       '. date('Y-m-d') ."\n";
  echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
  // or using strtotime():
  echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n";
?>

Changing + to - on the first (or last) line will get what you want.

Luís Guilherme