如何检查日期是否已过期

时间:2016-08-05 07:39:13

标签: php date datetime time mktime

我试图检查帖子是否已过期,这是我迄今为止尝试过的事情

$p = "08"; //suppose month is Aug
$m="2016"; //year is 2016
$input_time = mktime(0,0,0,$p+1,0,$m); 

if ($input_time < time()){
   print '<p class = "error">Post has expired</p>';

}
else{
echo 'Post is still fresh';
}

在上面的示例中,我只能查看月份和时间。一年,我怎样才能查看日,月和日一年?

4 个答案:

答案 0 :(得分:1)

您可以使用DateTime

$date = new DateTime('2016-09', new DateTimeZone('Europe/London'));
var_dump($date->getTimestamp()<time()); //true

$date = new DateTime('2016-09', new DateTimeZone('Europe/London'));
var_dump($date->getTimestamp()<time()); //false

$date = new DateTime('2016-08-06', new DateTimeZone('Europe/London'));
var_dump($date->getTimestamp()<time()); //false

$date = new DateTime('2016-08-04', new DateTimeZone('Europe/London'));
var_dump($date->getTimestamp()<time()); //true

答案 1 :(得分:1)

使用strtotime

$curdate = strtotime('05-08-2016');
$mydate = strtotime('04-08-2016');

if($curdate > $mydate)
{
    echo 'Post is Expired';
}

答案 2 :(得分:0)

使用strtotime("input time");并将其用于检查

答案 3 :(得分:0)

如果我遇到了问题,解决方案就很容易了。只需在第五个参数中传递一天而不是零:

$d='12';
$input_time = mktime(0,0,0,$p+1,$d,$m);