我需要计算7天前使用PHP的时间戳,所以如果它现在是3月25日下午7:30,它将返回3月18日下午7:30的时间戳。在一周前获取PHP的时间戳吗?
我应该只从当前时间戳减去604800秒,还是有更好的方法?
我需要计算7天前使用PHP的时间戳,所以如果它现在是3月25日下午7:30,它将返回3月18日下午7:30的时间戳。在一周前获取PHP的时间戳吗?
我应该只从当前时间戳减去604800秒,还是有更好的方法?
strtotime("-1 week")
strtotime是你的朋友
echo strtotime("-1 week");
echo strtotime("-1 week");
上有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";
?>
下面的例子在第一行(或最后一行)上更改+到 - 会得到你想要的。
<?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);
@AndrewBarber答案可能已被编辑,但这个答案似乎并没有包含任何链接。 – starbeamrainbowlabs 2014-07-07 11:38:27
从PHP 5.2您可以使用DateTime:
$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
您有错字strotime strtotime .. :)太早了5年前..:D – 2015-09-10 10:14:53
@Yegya哈!修正:P – 2015-09-17 13:37:28