英文:
Convert ISOdate to phpdate for ACF custom field query
问题
我已经构建了一个自定义的海狸生成器(WordPress)模块。我正在通过Ajax获取帖子。我需要基于ACF自定义字段日期查询帖子。
我以ISO8601格式发布日期(例如2013-12-01T00:00:00-05:00)。在服务器端,我获取开始和结束日期。我将它们转换为ACF查询所需的格式 https://www.advancedcustomfields.com/resources/date-picker/
$start_date = date('Ymd', strtotime($_POST['start']));
$end_date = date('Ymd', strtotime($_POST['end']));
我运行查询,但没有得到任何结果。我将字符串打印出来,它们看起来是正确的。
如果我按照ACF文档中的示例设置日期 - 它可以工作(下面的代码)。所以我一定是错误地转换了ISO日期 $_POST['start']
。我如何转换ISODATE,以便在查询中使用?
function get_ajax_event_calendar_posts() {
$today = date('Ymd'); // 这个可以工作...
$args = array(
'post_type' => array('event'),
'meta_query' => array(
array(
'key' => 'start_date',
'compare' => '<=',
'value' => $today,
),
array(
'key' => 'end_date',
'compare' => '>=',
'value' => $today,
)
),
'post_status' => array('publish'),
'posts_per_page' => 100,
'nopaging' => true,
'order' => 'DESC',
'orderby' => 'date'
);
// 查询
$ajaxposts = get_posts( $args );
//... 等等
}
编辑
.... 日期并不是问题。我才是问题... 将比较条件反过来,一切都正常工作...
英文:
I've built a custom beaver builder (wordpress) module. I'm fetching posts via Ajax. I need to query posts based off an ACF custom field date.
I am posting the date in ISO8601 format (eg 2013-12-01T00:00:00-05:00). Server side, I grab the start and end. I convert them into the format needed for the ACF query https://www.advancedcustomfields.com/resources/date-picker/
$start_date = date('Ymd', strtotime($_POST['start']));
$end_date = date('Ymd', strtotime($_POST['end']));
I run the query, and get nothing. I echo the string out, and they look correct.
If I set the date as per the example in the ACF docs - it works (code below). So I must be converting the ISOdate $_POST['start']
incorrectly. How do I convert the ISODATE so that is it something that I can use in the query?
function get_ajax_event_calendar_posts() {
$today = date('Ymd'); // this works...
$args = array(
'post_type' => array('event'),
'meta_query' => array(
array(
'key' => 'start_date',
'compare' => '<=',
'value' => $today,
),
array(
'key' => 'end_date',
'compare' => '>=',
'value' => $today,
)
),
'post_status' => array('publish'),
'posts_per_page' => 100,
'nopaging' => true,
'order' => 'DESC',
'orderby' => 'date'
);
// The Query
$ajaxposts = get_posts( $args );
//... etc
}
** edit **
.... the date stuff wasn't the problem. I was the problem... switched my compares round the right way and all works...
答案1
得分: 1
你没有说明实际需要存储数据的格式,但你提到了 date('Ymd')
可行。无论如何,可以使用 DateTime 类:
<?php
$x = new DateTime('2013-12-01T00:00:00-05:00');
echo $x->format('d/m/Y H:i:s') . "\n"; // 01/12/2013 00:00:00
echo $x->format('dmY') . "\n"; // 01122013
这里是日期格式的链接:https://www.php.net/manual/en/function.date.php
英文:
You haven't said what format you actually need to store the data, however you did say date('Ymd')
works. Either way, use the DateTime class:
<?php
$x = new DateTime('2013-12-01T00:00:00-05:00');
echo $x->format('d/m/Y H:i:s') . "\n"; // 01/12/2013 00:00:00
echo $x->format('dmY') . "\n"; // 01122013
Here are the date formats https://www.php.net/manual/en/function.date.php
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论