使用php,我应该如何从数组中获取即将到来的日期并将其传递给javascript?(Using php, how should I get the upcoming date from an arra

编程入门 行业动态 更新时间:2024-10-27 18:34:18
使用php,我应该如何从数组中获取即将到来的日期并将其传递给javascript?(Using php, how should I get the upcoming date from an array and pass it to javascript?)

我为WordPress设置了一个循环,它从自定义字段(日期选择器)获取日期并将其传递给数组。 我需要确定该阵列中未来最近的日期(即将到来的日期)。

<?php $today = current_time('m/d/Y'); $args=array( 'meta_key' => 'opening', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => -1, 'post_type' => 'event', ); $query = new WP_Query($args); ?> <?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $date = DateTime::createFromFormat('Ymd', get_field('opening')); $dates[] = $date->format('m/d/Y'); endwhile; endif; wp_reset_postdata(); print_r($dates); ?>

并将其传递给javascript。

$('#tl').timeline({ startItem : '$resultOfUpcomingDateEvaluation', });

请指教,谢谢你的时间。

I have setup a loop for WordPress which gets the date from a custom field (date picker) and passes it to a array. I need to determine the closest date in the future (upcoming date) from that array.

<?php $today = current_time('m/d/Y'); $args=array( 'meta_key' => 'opening', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => -1, 'post_type' => 'event', ); $query = new WP_Query($args); ?> <?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $date = DateTime::createFromFormat('Ymd', get_field('opening')); $dates[] = $date->format('m/d/Y'); endwhile; endif; wp_reset_postdata(); print_r($dates); ?>

And pass it to javascript.

$('#tl').timeline({ startItem : '$resultOfUpcomingDateEvaluation', });

Please advice, thanks for the time.

最满意答案

找到一个解决方案,有点脏,如果有人有一个更优雅的一个将是很好的。

我用类似的设置写了第二个循环,将帖子限制为1并将meta_value与获得current_time的var进行比较。 然后我回应结果将它包装在div中,并使用隐藏它的类(不优雅)。

<?php $today = current_time('Ymd'); $args=array( 'meta_key' => 'opening', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => 1, 'post_type' => 'event', 'meta_query' => array( 'key' => 'opening', 'compare' => '>=', 'value' => $today, ) ); $query = new WP_Query($args); ?> <?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $date = DateTime::createFromFormat('Ymd', get_field('opening')); $upcoming = $date->format('d/m/Y'); endwhile; endif; wp_reset_postdata(); echo '<div class="hide" id="upcomingdiv">'; echo $upcoming; echo '</div>'; ?>

之后,在我的javascript文件中,我通过ID获取元素并获取包含循环结果的div的.textContent并将其存储到变量中。

var setupUpcoming = document.getElementById("upcomingRef"); var upcoming = setupUpcoming.textContent;

最后我将变量传递给startItem。

$('#tl').timeline({ startItem : upcoming, });

Found a solution, little dirty so will be nice if someone has a more elegant one.

I wrote a second loop with a similar setup, limiting the posts to 1 and comparing the meta_value with a var that gets current_time. Then i echo the result wrapping it in a div with a class that hides it (not elegant).

<?php $today = current_time('Ymd'); $args=array( 'meta_key' => 'opening', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => 1, 'post_type' => 'event', 'meta_query' => array( 'key' => 'opening', 'compare' => '>=', 'value' => $today, ) ); $query = new WP_Query($args); ?> <?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php $date = DateTime::createFromFormat('Ymd', get_field('opening')); $upcoming = $date->format('d/m/Y'); endwhile; endif; wp_reset_postdata(); echo '<div class="hide" id="upcomingdiv">'; echo $upcoming; echo '</div>'; ?>

After that, on my javascript file, i get the element by ID and get the .textContent of the div that contains the result of the loop and store it to a variable.

var setupUpcoming = document.getElementById("upcomingRef"); var upcoming = setupUpcoming.textContent;

Finally i pass the variable to the startItem.

$('#tl').timeline({ startItem : upcoming, });

更多推荐

本文发布于:2023-08-01 00:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   日期   并将其   php   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!