将ISO日期转换为用于ACF自定义字段查询的PHP日期。

huangapple go评论88阅读模式
英文:

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(&#39;Ymd&#39;, strtotime($_POST[&#39;start&#39;]));
$end_date = date(&#39;Ymd&#39;, strtotime($_POST[&#39;end&#39;])); 

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[&#39;start&#39;] 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(&#39;Ymd&#39;); // this works...

    $args = array(
        &#39;post_type&#39; =&gt; array(&#39;event&#39;),
        &#39;meta_query&#39; =&gt; array(
            array(
                &#39;key&#39;           =&gt; &#39;start_date&#39;,
                &#39;compare&#39;       =&gt; &#39;&lt;=&#39;,
                &#39;value&#39;         =&gt; $today,
            ),
            array(
                &#39;key&#39;           =&gt; &#39;end_date&#39;,
                &#39;compare&#39;       =&gt; &#39;&gt;=&#39;,
                &#39;value&#39;         =&gt; $today,
            )
        ),
        &#39;post_status&#39; =&gt; array(&#39;publish&#39;),
        &#39;posts_per_page&#39; =&gt; 100,
        &#39;nopaging&#39; =&gt; true,
        &#39;order&#39; =&gt; &#39;DESC&#39;,
        &#39;orderby&#39; =&gt; &#39;date&#39;
    );
    // 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(&#39;Ymd&#39;) works. Either way, use the DateTime class:

&lt;?php

$x = new DateTime(&#39;2013-12-01T00:00:00-05:00&#39;);
echo $x-&gt;format(&#39;d/m/Y H:i:s&#39;) . &quot;\n&quot;;     // 01/12/2013 00:00:00
echo $x-&gt;format(&#39;dmY&#39;) . &quot;\n&quot;;     // 01122013

Here are the date formats https://www.php.net/manual/en/function.date.php

huangapple
  • 本文由 发表于 2020年1月6日 18:29:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定