admin管理员组

文章数量:1122832

So I am making changes to a clients website and currently they have an events post type with some custom fields that allow them to set an events end date/time and the events timezone.

The custom fields setup:

  • Start Date and Time: text input / saves as php format U
  • End Date and Time: text input / saves as php format U
  • Timezone: select field / saves as string (e.g. GMT / BST)

And then on the front end their current wp_query is:

$events_query = new WP_Query([
    'post_type'     => 'events',
    'meta_key'      => 'start_date_and_time',
    'orderby'       => 'meta_value_num',
    'order'         => 'ASC',
    'meta_query'    => [
        [
            'key'       => 'end_date_and_time',
            'value'     => date('U'),
            'compare'   => '>=',
            'type'      => 'NUMERIC'
        ]
    ]
]);

But this is not taking into account the timezone set in the backend compared to the timezone of the user.

Any help would be great.

Thanks.

So I am making changes to a clients website and currently they have an events post type with some custom fields that allow them to set an events end date/time and the events timezone.

The custom fields setup:

  • Start Date and Time: text input / saves as php format U
  • End Date and Time: text input / saves as php format U
  • Timezone: select field / saves as string (e.g. GMT / BST)

And then on the front end their current wp_query is:

$events_query = new WP_Query([
    'post_type'     => 'events',
    'meta_key'      => 'start_date_and_time',
    'orderby'       => 'meta_value_num',
    'order'         => 'ASC',
    'meta_query'    => [
        [
            'key'       => 'end_date_and_time',
            'value'     => date('U'),
            'compare'   => '>=',
            'type'      => 'NUMERIC'
        ]
    ]
]);

But this is not taking into account the timezone set in the backend compared to the timezone of the user.

Any help would be great.

Thanks.

Share Improve this question asked Jun 23, 2016 at 9:45 Levi ColeLevi Cole 6232 gold badges6 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

date() PHP functions returns the value of time() PHP functions in the specified format. time() use server local time. If you want to get date/time based of WordPress configuration, you could use current_time(), a WordPress function, instead of native PHP functions.

// 'timestamp' = Unix Timestamp or 'U' PHP time format
$current_time = current_time( 'timestamp' );

本文标签: Query events post type after current date and timezone