0

This question already has an answer here:

I have a template that displays an actual event if we have a today one, or a next future one. The template uses this WP_Query, that compares events date with the actual date:

$args = array(
    'post_type' => 'events', // Tell WordPress which post type we want
    'orderby' => 'meta_value', // We want to organize the events by date    
    'meta_key' => 'event_start_date', // Grab the "start date" field created via a third party plugin (stored in YYYY-MM-DD format)
    'order' => 'ASC', // ASC is the other option    
    'posts_per_page' => '1', // Let's show only one / the first event.   
    'meta_query' => array( // WordPress has all the results, now, return only the events after today's date
        array(
            'key' => 'event_start_date', // Check the start date field
            'value' => date_i18n("Y-m-d"), // Set today's date (note the similar format)
            'compare' => '>=', // Return the ones greater than or equal to today's date
            'type' => 'DATE' // Let WordPress know we're working with date
        )
    )
);

$featured = new WP_Query( $args );

This works, but I found a problem. Accordingly to this answer, WordPress sets the PHP timezone to UTC for internal calculations. In my database events date are saved in a "Y-m-d" date format (without time). So, this means that at 2017-08-31 00:01 date/time (24h format) in my local timezone (UTC+2) the WP will compare a saved event date, say 2017-08-31, with 2017-08-30 of his internal UTC timezone (2 hours difference!) and still will display an old event instead of new one in these 2 hours after 2017-08-31 00:00 in my local time. How to solve this?

UPDATE

Changed the code as was recommended by @Milo. The problem is solved!

UPDATE - About duplicate

Guys, I suppose that for you my question is a duplicate of an existing question that already has a very clear answer, clear for you, but not also for me, as this answer doesn't have any example code. I seen that answer, if you have not noticed, and I put a link to it in my question before you, but this doesn't helped me much. I needed an exact solution for my situation, and @milo provided it for me, so I thank you him!

marked as duplicate by Milo, Tom J Nowell Aug 31 '17 at 0:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • the answer you linked has a solution. – Milo Aug 30 '17 at 23:04
  • I don't know how to use that solution :(. – Iurie Malai Aug 30 '17 at 23:05
  • 1
    replace date("Y-m-d") with date_i18n("Y-m-d") – Milo Aug 30 '17 at 23:06
  • Tested right now, it works! Thank you very much! BTW, I found that the time discrepancy actually was more than 2 hours. Can't explain this! – Iurie Malai Aug 30 '17 at 23:14
  • 1
    the timestamp in UTC represents the moment in time universally, not the what it says on the clocks in the UTC timezone. That's purely a coincidence. WP stores everything in UTC regardless of timezone, and translates to the preferred timezone, so you need to convert to UTC when saving, and then back to UTC+2 when displaying, which is exactly what WP already does. Otherwise there'd be massive confusion when it came to sites that store multiple timezones, or users in multiple timezones. e.g. what happens if a user on holiday in UTC+4 makes a post and there's no conversion? – Tom J Nowell Aug 31 '17 at 12:53