这个数字你得到的,所谓的,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
阅读
documentation日期功能
的想法与比较日期时,您曾是非常糟糕的,让我们假设今天是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) {
}
阅读'日期()'函数的文档。并且习惯于随时阅读文档。这个数字被称为UNIX时间戳,可以用来轻松地使用日期进行计算。 – kapa 2013-03-11 09:12:09