我正在使用PHP制作wordpress插件。目标是插件将运行到指定的日期,而不是停止。
问题是,比方说我说过,截止日期是2012年9月16日。系统只会在2012年9月17日上午08:00停止插件。我怎样才能让它在2012年9月17日凌晨12点停下来。
对应的编码如下所示。需要你的建议。谢谢
function display($content) {
$exp_date = "16-09-2012";
$todays_date = date("d-m-Y");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date >= $today) {
return flag().$content;
} else {
return $content;
}
}
最好使用"mktime()"来制作到期日期的时间戳。然后,您可以与当前时间戳进行比较,您可以通过函数"time()"获得该时间戳。
例如
$exp_date = mktime(23,59,59,9,16,2012);
if(time() > $exp_date){
// expired
} else {
// Not expired.
}
$exp_date = "16-09-2012";
$todays_date = date("d-m-Y");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
可以提高的可读性和舒适性
$exp_date = "16-09-2012";
$today = new DateTime('now', new DateTimezone('UTC'));
$expiration_date = new DateTime($exp_date,new DateTimezone('UTC');//can be other timezone
使用DateTime,您可以与>、<、>=进行比较<=就像使用时间戳一样,但您使用的是具有"日期"含义的东西,而不是整数。