2010-03-24 46 views

回答

20

strtotime是你的朋友

echo strtotime("-1 week"); 
+0

您有错字strotime strtotime .. :)太早了5年前..:D – 2015-09-10 10:14:53

+0

@Yegya哈!修正:P – 2015-09-17 13:37:28

6

上有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"; 
?> 

下面的例子在第一行(或最后一行)上更改+到 - 会得到你想要的。

-1
<?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); 
+1

@AndrewBarber答案可能已被编辑,但这个答案似乎并没有包含任何链接。 – starbeamrainbowlabs 2014-07-07 11:38:27

3

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