我需要使用PHP计算七天前的时间戳,例如现在是3月25日晚上7点30分,它应该返回3月18日晚上7点30分的时间戳。
我只需从当前时间戳减去604800秒,或者有更好的方法吗?
strtotime("-1 week")
strtotime 是你的好朋友
echo strtotime("-1 week");
<?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";
?>
$timestring="2015-03-25";
$datetime=new DateTime($timestring);
$datetime->modify('-7 day');
echo $datetime->format("Y-m-d"); //2015-03-18
不要使用字符串创建DateTime
,你可以在对象上直接setTimestamp:
$timestamp=1427241600;//2015-03-25
$datetime=new DateTime();
$datetime->setTimestamp($timestamp);
$datetime->modify('-7 day');
echo $datetime->format("Y-m-d"); //2015-03-18
<?php
$before_seven_day = $date_timestamp - (7 * 24 * 60 * 60)
// $date_timestamp is the date from where you found to find out the timestamp.
?>
你也可以使用字符串转时间函数将日期转换为时间戳。例如:
strtotime(23-09-2013);