Today condition doesn't handle time zones properly

Hi,

My WordPress install’s local time zone is GMT-0500. I verified this by visiting the general settings page, which shows the correct universal and local time.

If the current local time is Fri Dec 10 2021 13:22:00 and I put a Today after condition on an element, the element is visible when the condition’s date/time is Fri Dec 10 2021 08:20:00 GMT-0500 but hidden when the condition’s date/time is Fri Dec 10 2021 08:25:00 GMT-0500.

It’s also visible when the condition’s date/time is Fri Dec 10 2021 13:20:00 GMT-0000 but hidden when the condition’s date/time is Fri Dec 10 2021 13:25:00 GMT-0000.

In the global_today function, you’re comparing current_time('timestamp') to strtotime( $date ). The strtotime part is fine, I think, because strtotime correctly uses the GMT-0500 part of the string to adjust for the time zone and return a UTC timestamp. But WordPress’s current_time function doesn’t return a UTC timestamp unless you pass true as its second parameter.

Without that parameter, it gets time(), which is a UTC timestamp, and then it adds get_option( 'gmt_offset' ) * HOUR_IN_SECONDS to it. In my case, it adds -18000 seconds to the UTC timestamp of the current time. Then, Cornerstone compares that value to the UTC timestamp of the date/time when the element is supposed to appear, so the element appears at the wrong time.

If I add true as the second parameter of the current_time('timestamp') function call, the condition starts behaving as expected.

PHP 7.4 (and PHP 8.0)
WordPress 5.8.2
Cornerstone 6.1.4

Thanks!

Hi @fundibu,

Thanks for reaching out.
I have checked this in the local environment, but was unable to replicate it. Can you please provide login credentials for your site in a secure note to examine it further, including:

– WordPress Site URL & Login URL
– WordPress Admin username/password

To create a secure note, click the key icon underneath any of your posts.

NOTE: I will also share your observation with our development team to check it.

Thanks

Hi Tristup,

Thanks for replying! I added a secure note to my original post.

Hi @fundibu,

I have checked your website and created a Test page and found that the Today condition is working with the available Date and Time options. If you are referring to any specific condition, please let us know the page and the section where you have implemented it.

Thanks

Hi Tristup,

Unfortunately, I can’t set up a specific test case for you to look at, because any given condition would only apply at a certain time, and I don’t know when you’re going to look at it. But I can give you another example that might help you reproduce the problem.

Based on some information I found by Googling your name, and based on the fact that your replies in this thread have been posted at a very early hour (in my time zone), I’ll assume you’re in the IST time zone (GMT+0530). I configured the site to use that time zone, then performed another test.

When I performed this test, the actual date/time was Tue Dec 14 2021 21:05:00 GMT+0530.

I created a Today after condition on a section and set the date/time string to Tue Dec 14 2021 21:10:00 GMT+0530. The section should not have been visible, because 21:05 is not after 21:10. But it was visible. It was also visible when I set the condition’s date/time to Wed Dec 15 2021 02:30:00 GMT+0530. It did not disappear until I set the condition’s date/time to Wed Dec 15 2021 02:35:00 GMT+0530 or later. That means the section would have appeared 5.5 hours earlier than expected.

(If you aren’t in the IST time zone, feel free to change the test site’s time zone setting to match your time zone, then try something similar to the above example.)

My real websites are configured to use the GMT-0500 time zone. Because that time zone has a negative offset instead of a positive offset, content is appearing later than expected instead of earlier than expected, but the principle is the same.

As I said in my first post, I think the problem is that WordPress’s current_time function, by default, gets the current UTC time, then adds the site’s time zone offset to it, which means the value it returns is not the current UTC time (unless the site happens to be configured to use a UTC±0 time zone). Then, Cornerstone compares this non-UTC time to the UTC time returned by strtotime. Adding true as the second argument to current_time should fix it.

Thanks for bearing with me!

Hey @fundibu,

I have replicated the issue in my test site so I’m going to post this in our issue tracker for investigation.

For now, please don’t use the Global Today.

image

Instead, choose Datetime and manually configure the times on the left and right sides of the condition

image

The manual Dynamic Content could look like this {{dc:global:date format="D M j Y H:i:s"}} and it’s output would look like this Tue Dec 21 2021 22:49:00. Notice that it does not have the GMT.

image

That works as I’ve tested that on your site.

Hope that helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.