日期检查总是返回false
我有一个自定义字段expirydate是由acf作为日期选择器...与下面的代码我试图删除帖子tht已过期...由于某些原因代码不起作用。日期检查总是返回false
$arg= get_posts(array('post_type' => tokens, 'numberposts' => -1));
foreach ($arg as $post) : setup_postdata($post);
if (!empty($post))
{
$expiry=get_field('expiry',$post->ID);
$today=date("d/m/Y");
if ($today>$expiry) {
echo $expiry."expired <br>";
wp_delete_post($postid);
}
else {
echo "not expired"."<br>";
}
}
endforeach;
echo "process completed";
?>
条件$today>$expiry
总是返回false
你比较串,不日期。因此31/1/2014
大于01/12/2014
,因为3> 1时比较字符串。
要正确比较日期,请使用可正确比较的字符串格式或使用DateTime()
可比较的对象。
$expiry = DateTime::createFromFormat('d/m/y', get_field('expiry',$post->ID));
$today = new DateTime();
if ($today > $expiry) {
我建议不要使用strtotime()
日期比较,因为它确实不采取夏令考虑。
伟大的建议,不知道。 – FloydThreepwood 2015-02-08 01:46:20
它给了我一个错误... PLZ不是使用ACF保存在帖子中的expirydate格式为“dd/mm/yy” – user3117694 2015-02-08 01:47:31
可以帮助我http://support.advancedcustomfields.com/forums/topic/list -all-values-in-select-field/ – user3117694 2015-02-08 01:53:34
我会将字符串转换为UNIX时间戳并进行比较。您可以使用此功能转换http://php.net/manual/ru/function.strtotime.php – Tamara 2015-02-08 01:38:18