如何只保留日期介于其他日期之间的帖子? [英] How to only keep posts with dates in between other dates?

查看:21
本文介绍了如何只保留日期介于其他日期之间的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有一些自定义字段的帖子,其中我有一个字符串格式的日期,所以我循环我的帖子并将它们中的每一个都转换为日期,然后我做了一些按日期排序的操作:

I have some posts with some custom fields where I have a date in a string format, so I loop my posts and I convert each of them as a Date, then I do some bit to have them in order by date:

foreach($posts as $post) {
    $postid = $post->ID;
    $myDate = \DateTime::createFromFormat('j-n-Y', get_post_meta($postid, 'usp-custom-80', true));
    $postOrdered[$postid] = \DateTime::createFromFormat('j-n-Y', get_post_meta($postid, 'usp-custom-80', true));
    wp_reset_query();
}
arsort($postOrdered);
var_export($postOrdered);

所以我将日期与 ID 附加在一起,这给了我们:

So I am attaching the date together with the ids and that gives us:

array ( 128288 => DateTime::__set_state(array( 'date' => '2017-08-20 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128166 => DateTime::__set_state(array( 'date' => '2017-08-17 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128308 => 
DateTime::__set_state(array( 'date' => '2017-08-05 20:36:02.000000', DateTime::__set_state(array( 'date' => '2000-03-20 20:36:02.000000', 
DateTime::__set_state(array( 'date' => '1978-05-11 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128295 => 
DateTime::__set_state(array( 'date' => '1978-04-10 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128337 =>
 DateTime::__set_state(array( 'date' => '1978-03-16 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128315 => DateTime::__set_state(array( 'date' => '1976-08-10 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128290 => DateTime::__set_state(array( 'date' => '1970-04-12 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), )

好的,现在我们要做的是使用 GET

OK now what we do is GET some $start and $end dates from a form using GET

$start = $_GET['start']; 
$end = $_GET['end'];
$start  = \DateTime::createFromFormat('j-n-Y', $start);
$end  = \DateTime::createFromFormat('j-n-Y', $end);
var_dump($start);
var_dump($end);

这给了我们:

object(DateTime)#8267 (3) { ["date"]=> string(26) "2019-02-01 20:51:56.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } 
object(DateTime)#7974 (3) { ["date"]=> string(26) "2019-02-05 20:51:56.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }

现在我要做的是简单检查这些帖子中是否有任何在我们的 $start 和 $end 之间有任何日期并显示它们的订单 ASC 或 DESC

Now what i am trying to do is a simple check if any of those posts have any date in between our $start and $end and display them order ASC or DESC

有点像

if ( ( $myDate >= $start) && ( $myDate <= $end) ) {
 ...my posts...

但是不管怎样,这仍然给了我们所有的帖子

But that still gives us all posts regardless

推荐答案

你可以直接比较 DateTime 对象,所以你的代码可以写成:

You can just compare DateTime objects directly, so your code can be written as:

$start = \DateTime::createFromFormat('j-n-Y', $_GET['start']);
$end = \DateTime::createFromFormat('j-n-Y', $_GET['end']);
foreach($posts as $post) {
    $postid = $post->ID;
    $myDate = \DateTime::createFromFormat('j-n-Y', get_post_meta($postid, 'usp-custom-80', true));
    if (($myDate >= $start) && ($myDate <= $end)) {
        $postOrdered[$postid] = (int)$myDate->format('U');
    }
    wp_reset_query();
}

请注意,您帖子上的所有日期都在 2019 年之前,因此您需要调整 $start 值以查看输出中的所有帖子.

Note that all the dates on your posts are before 2019, so you will need to adjust your $start value to see any posts in the output.

要获得唯一日期的排序列表,请在 foreach 循环后使用以下代码:

To get a sorted list of unique dates, use this code after the foreach loop:

$postOrdered = array_unique($postOrdered);
arsort($postOrdered);

这篇关于如何只保留日期介于其他日期之间的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆