PHP 日期 (ymd) 比较错误
我正在使用日期选择器来存储用户选择的 unix / PHP 时间戳。
我刚刚注意到,在我尝试打印 $current_date
并将其与 $_date_compare
进行比较时......仅供参考,这是 WordPress 代码,但问题与 WordPress 无关。
因此,这里是相关代码片段:
$current_date = date('ymd');
$recentPosts = new WP_Query();
$recentPosts->query(array(
'category_name' => 'events',
'meta_key' => '_date_compare',
'meta_compare' => '>=',
'meta_value' => $current_date,
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 99
));
出于某种原因,2005 年 1 月 1 日的结果是打印 50101(省略了前导零,这让我很困惑,因为我使用的是 date('ymd'); 尽管如此,经过仔细思考,
我意识到这不是问题,但我正在尝试对具有高于 $current_date
值的所有事件进行排序。该查询在 HIGHER Than $current_date
查询中返回 2005 年 1 月 1 日的日期。
在撰写本文时,今天是 date('ymd' 中的 110523)。 )
格式。为什么在检查高于值的查询中返回50101?
编辑 您可以在此处查看问题:http://anasmadance.com/past-events -2/ - 方框上方打印的数字是 ymd
格式的日期,对于较早的日期来说是正确的,但对于页面底部的日期来说是不正确的(应该从查询中省略,因为它们已经过去,因此小于它们所比较的值)
I am using a datepicker to store a unix / PHP timestamp that is selected by the user.
I just noticed that in my attempt to print $current_date
and compare it against $_date_compare
... FYI this is WordPress code, but the problem is not related to WordPress.
So, that being said here's the snippet of relevant code:
$current_date = date('ymd');
$recentPosts = new WP_Query();
$recentPosts->query(array(
'category_name' => 'events',
'meta_key' => '_date_compare',
'meta_compare' => '>=',
'meta_value' => $current_date,
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 99
));
For some reason, the result for Jan 01, 2005 is printing 50101 (leaving out the leading zero, which confuses me since I am using date('ymd');
format for the date.
Nevertheless, after some careful thought I realized that this is a non-issue. I'm trying to sort all events that have a HIGHER value than $current_date
, yet the query is returning the Jan 01 2005 date in the HIGHER than $current_date
query.
At the time of this writing, today is 110523 in date('ymd')
format. Why is 50101 being returned in my query that is checking for values that are higher than 110523?!
EDIT You can view the problem here: http://anasmadance.com/past-events-2/ - The numbers printed just above the boxes are meant to be ymd
formatted dates, which are correct for the early dates, but incorrect for the ones at the bottom of the page (which should be omitted from the query since they are past and therefore less than the value they are compared against)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否可以帮助你。我需要查看并进行多次调试才能知道发生了什么,但这是我通过谷歌搜索发现的
尝试用 $wp_query 替换 $recentPosts 并告诉我们结果是什么。我知道这看起来很愚蠢,但这就是我发现的声明:
http://wordpress.org/support/topic/wp_query -amp-pagination-not-working?replies=11
在这里:
http://wordpress.org/support/topic/custom-wp_query-with-paged
如果这不起作用,请您在其后立即 var_dump $current_date 并让我们知道结果。
I don't know if this may help you. I need to see and do several debugging to know what's going on, but here is what I found by googling
try to replace $recentPosts with $wp_query and tell us what's the result. I know this looks silly, but this is what I found claimed here:
http://wordpress.org/support/topic/wp_query-amp-pagination-not-working?replies=11
and here:
http://wordpress.org/support/topic/custom-wp_query-with-paged
if this didn't work, can you please var_dump $current_date right after it and let us know the result.