2011-02-16 90 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) { 
    ... 
}