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

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

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/

  1. $start_date = date('Ymd', strtotime($_POST['start']));
  2. $end_date = date('Ymd', strtotime($_POST['end']));

我运行查询,但没有得到任何结果。我将字符串打印出来,它们看起来是正确的。

如果我按照ACF文档中的示例设置日期 - 它可以工作(下面的代码)。所以我一定是错误地转换了ISO日期 $_POST['start']。我如何转换ISODATE,以便在查询中使用?

  1. function get_ajax_event_calendar_posts() {
  2. $today = date('Ymd'); // 这个可以工作...
  3. $args = array(
  4. 'post_type' => array('event'),
  5. 'meta_query' => array(
  6. array(
  7. 'key' => 'start_date',
  8. 'compare' => '<=',
  9. 'value' => $today,
  10. ),
  11. array(
  12. 'key' => 'end_date',
  13. 'compare' => '>=',
  14. 'value' => $today,
  15. )
  16. ),
  17. 'post_status' => array('publish'),
  18. 'posts_per_page' => 100,
  19. 'nopaging' => true,
  20. 'order' => 'DESC',
  21. 'orderby' => 'date'
  22. );
  23. // 查询
  24. $ajaxposts = get_posts( $args );
  25. //... 等等
  26. }

编辑
.... 日期并不是问题。我才是问题... 将比较条件反过来,一切都正常工作...

英文:

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/

  1. $start_date = date(&#39;Ymd&#39;, strtotime($_POST[&#39;start&#39;]));
  2. $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?

  1. function get_ajax_event_calendar_posts() {
  2. $today = date(&#39;Ymd&#39;); // this works...
  3. $args = array(
  4. &#39;post_type&#39; =&gt; array(&#39;event&#39;),
  5. &#39;meta_query&#39; =&gt; array(
  6. array(
  7. &#39;key&#39; =&gt; &#39;start_date&#39;,
  8. &#39;compare&#39; =&gt; &#39;&lt;=&#39;,
  9. &#39;value&#39; =&gt; $today,
  10. ),
  11. array(
  12. &#39;key&#39; =&gt; &#39;end_date&#39;,
  13. &#39;compare&#39; =&gt; &#39;&gt;=&#39;,
  14. &#39;value&#39; =&gt; $today,
  15. )
  16. ),
  17. &#39;post_status&#39; =&gt; array(&#39;publish&#39;),
  18. &#39;posts_per_page&#39; =&gt; 100,
  19. &#39;nopaging&#39; =&gt; true,
  20. &#39;order&#39; =&gt; &#39;DESC&#39;,
  21. &#39;orderby&#39; =&gt; &#39;date&#39;
  22. );
  23. // The Query
  24. $ajaxposts = get_posts( $args );
  25. //... etc
  26. }

** 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 类:

  1. <?php
  2. $x = new DateTime('2013-12-01T00:00:00-05:00');
  3. echo $x->format('d/m/Y H:i:s') . "\n"; // 01/12/2013 00:00:00
  4. 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:

  1. &lt;?php
  2. $x = new DateTime(&#39;2013-12-01T00:00:00-05:00&#39;);
  3. echo $x-&gt;format(&#39;d/m/Y H:i:s&#39;) . &quot;\n&quot;; // 01/12/2013 00:00:00
  4. 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:

确定