在PHP中获取一周前的时间戳?

Mik*_*den 38 php datetime timestamp

我需要使用PHP计算7天前的时间戳,所以如果它现在是3月25日晚上7:30,它将返回3月18日晚上7:30的时间戳.

我应该从当前时间戳中减去604800秒,还是有更好的方法?

Ben*_*ard 26

strtotime是你的朋友

echo strtotime("-1 week");
Run Code Online (Sandbox Code Playgroud)


Luí*_*rme 9

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";
?>
Run Code Online (Sandbox Code Playgroud)

在第一行(或最后一行)上更改+到 - 将获得您想要的结果.