2010-03-24 42 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