2011-02-16 93 views
0

我試圖用PHP中的unix時間戳計算過去3周的第一天和最近3個月的第一天。最近3周的第一天和最近3個月的第一天

我知道我必須使用日期功能,但有點丟失。我沒有PHP 5.3,因此我不能使用相對格式。

正在使用上述內容來決定是否刪除備份。例如

if ($time > time()-3600*24*7 && $time < time()) { 
    echo "Keeping: $file<br/>"; 
} 

我想保留的備份:

  1. 最後7天
  2. 過去的3周的第一天
  3. 的最近3個月

我試圖第一天在PHP中使用unix時間戳計算過去3周的第一天和最近3個月的第一天。

我知道我必須使用日期功能,但有點丟失。我沒有PHP 5.3,因此我不能使用相對格式。

正在使用上述內容來決定是否刪除備份。例如

if ($time > time()-3600*24*7 && $time < time()) { 
    echo "Keeping: $file<br/>"; 
} 

我想保留的備份:

  1. 最後7天
  2. 過去的3周的第一天
  3. 的最近3個月第一天

更新

添加解決方案,我想它在Pekka的幫助下退出

$a = (strtotime("last Monday-1 week")); 
$b = (strtotime("last Monday-2 week")); 
$c = (strtotime("last Monday-3 week")); 
$d = (strtotime(date('m').'/01/'.date('Y').' 00:00:00')); 
$e = (strtotime('-1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))); 
$f = (strtotime('-2 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))); 

回答

0

使用strtotime()的魔法。

echo strtotime("-7 days"); 
echo strtotime("-3 weeks"); 
echo strtotime("-3 months"); 

的strtotime()甚至可以做的東西一樣last tuesdaymidnight .....

+0

date('lsS \ of FY h:i:s A',strtotime(「 - 1 m onth「))給出了1月16日,但我想要1月1日的任何想法? – 2011-02-16 11:55:24

0

要計算日期,您可以使用strtotime

$last7days = strtotime('-7 days'); 
$last3weeks = strtotime('-3 weeks'); 
$last3months = strtotime('-3 months'); 

然後簡單地把它比作$time值:

if ($time > $last7days) { 
    ... 
}