0

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.

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.