简体   繁体   English

PHP WordPress - 检查日期是否在过去 30 天内

[英]PHP WordPress - check if a date falls within the last 30 days

I've been handed a WordPress file that displays custom events.我收到了一个显示自定义事件的 WordPress 文件。 It's been setup so that events are split into 2 categories based on if the date assigned to the event is in the past or future.它已被设置,以便根据分配给事件的日期是过去还是将来,将事件分为 2 个类别。

What I've been asked to do is add a 3rd category of "Recently Past" ( If the event happened within the past 30 days from today's date ).我被要求做的是添加第三类“最近过去”(如果事件发生在从今天开始的过去 30 天内)。

I've looked at other answers on here like this one , which makes me think I need to use something like 'value' => date('Ymd', strtotime('today - 30 days')), but if this is correct, how would I stop the "Recently Past" events from also showing up in the "Past" events category?我在这里看过其他答案,比如这个,这让我觉得我需要使用类似'value' => date('Ymd', strtotime('today - 30 days')),但如果这是正确的,如何阻止“最近过去”事件也出现在“过去”事件类别中?

In case it's helpful, it's also using Advanced Custom Fields as part of the query, which you can see in the code below if ( get_sub_field('show_events') == 'past' ) { I know how to add an additional query there, so can amend that if block to have an extra query.如果它有帮助,它还使用高级自定义字段作为查询的一部分,您可以在下面的代码中看到if ( get_sub_field('show_events') == 'past' ) {我知道如何在那里添加额外的查询,所以可以修改if块以获得额外的查询。 It's the array comparisons I'm stuck on.这是我坚持的数组比较。

<?php 

// Date is past                                
$meta_query_past = array(
  array(
    'key' => 'event_date',
    'value' => date('Ymd'),
    'type' => 'DATE',
    'compare' => '<='
  )
);

// Date is future
$meta_query_future = array(
  array(
    'key' => 'event_date',
    'value' => date('Ymd'),
    'type' => 'DATE',
    'compare' => '>='
  )
);

if ( get_sub_field('show_events') == 'past' ) {
  $meta_query = $meta_query_past;
  $attending = "We attended this event";
  $show_events = 'past';
  $sort_order = 'DESC';
} else {
  $meta_query = $meta_query_future;
  $attending = "We're attending this event";
  $show_events = 'future';
  $sort_order = 'ASC';
}

$args = array(
  'post_type' => 'event',
  'posts_per_page' => -1,
  'order' => $sort_order,
  'orderby' => 'meta_value_num',
  'meta_key' => 'event_date',
  'meta_query' => $meta_query
);

?>

Posting the solution to my own question just in case it helps anyone with a similar issue.将解决方案发布到我自己的问题,以防万一它对遇到类似问题的任何人有所帮助。

<?php 
// Date is past, but not within the last 30 days
$meta_query_past = array(
  array(
    'key' => 'event_date',
    'value' => date('Ymd', strtotime('-30 days')),
    'type' => 'DATE',
    'compare' => '<'
  )
);
// Date is recent
$meta_query_recent = array(
  'relation' => 'AND',
  // Date is more than date 30 days ago 
  array(
    'key' => 'event_date',
    'value' => date('Ymd', strtotime('-30 days')),
    'type' => 'DATE',
    'compare' => '>='
  ),
  // Date is less than todays date
  array(
    'key' => 'event_date',
    'value' => date('Ymd'),
    'type' => 'DATE',
    'compare' => '<'
  )
);
$meta_query_future = array(
  'relation' => 'OR', 
  // Date is future
  array(
    'key' => 'event_date',
    'value' => date('Ymd'),
    'type' => 'DATE',
    'compare' => '>='
  ),
  // Date is empty
  array(
    'key' => 'event_date',
    'value' => '',
    'compare' => '='
  )
);

if ( get_sub_field('show_events') == 'past' ) {
  $meta_query = $meta_query_past;
  $attending = "We attended this event";
  $show_events = 'past';
  $sort_order = 'DESC';
} elseif ( get_sub_field('show_events') == 'recent' ) {
  $meta_query = $meta_query_recent;
  $attending = "We recently attended this event";
  $show_events = 'recent';
  $sort_order = 'DESC';
} else {
  $meta_query = $meta_query_future;
  $attending = "We're attending this event";
  $show_events = 'future';
  $sort_order = 'ASC';
}

$args = array(
  'post_type' => 'event',
  'posts_per_page' => -1,
  'order' => $sort_order,
  'orderby' => 'meta_value_num',
  'meta_key' => 'event_date',
  'meta_query' => $meta_query
);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM