Today I spent a frustratingly amount of time trying to list upcoming Events Calendar events on a website, and getting them to stay listed through the start and end time of the event. The reason why events were falling off early is because of the timezone.
Events Calendar’s documentation (which I have found to be frustratingly lacking on numerous occasions) says to do the following:
// Retrieve the next 5 upcoming events /
$events = tribe_get_events(
array(
'posts_per_page' => 5,
'start_date' => date( 'Y-m-d H:i:s' ) ) );
$events = tribe_get_events( $args );
However, this does not take into consideration the timezone setting you set on the WordPress Settings page. To take the site’s timezone into account, you want to do the following:
// Retrieve the next 5 upcoming events /
$events = tribe_get_events(
array(
'posts_per_page' => 5,
'start_date' => date('Y-m-d H:i:s', current_time('timestamp', 0) );
$events = tribe_get_events( $args );
WordPress.org’s current_time(‘timestamp’, 0) documentation
The important bit of current_time function is: If 0 or no second parameter is set, the value returned represents the local time for the timezone declared in the blog’s Timezone setting on the General Settings page.
Note: if you debug and look at the query this creates, the meta_value of the start and end dates is in UTC time, which is how the event dates are stored in the database. However, you have to send the correct ‘current’ time to the query in order for tribe_get_events to convert the time to UTC correctly.