你得到的那个数字就是所谓的 unix 时间戳——自 01.01.1970 以来的秒数,主要是你应该用它来做你想做的事情:
$todaydate = time(); // same as strtotime('now'), but without overhead of parsing 'now'
$mydate = strtotime('20130311'); // turn your date into timestamp
$oneweekprior = $todaydate - 7*24*60*60; // today - one week in seconds
// or
//$oneweekprior = strtotime('-7 days');
if ($mydate > $oneweekprior && $mydate < $todaysdate) {
// do something
}
将时间戳转回人类可读的形式使用strftime
或date
功能:
echo strftime('%Y%m%d', $todaydate);
请阅读PHP 中日期函数的文档
像您想要的那样比较日期的想法非常糟糕,让我们假设今天是20130301
并且要检查的日期是20130228
- 使用您的解决方案它将是:
$mydate = 20130228;
$today = 20130301;
$weekago = $today - 7;
// $mydate should pass this test, but it won't because $weekago is equal 20130294 !!
if ($mydate > $weekago && $mydate < $today) {
}