Skip Navigation

[Resolved] Function not showing correct time via custom code snippets after start of DST

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user had some custom code below.

add_shortcode('time-date', 'timedate');
function timedate() {
date_default_timezone_set( get_post_meta(get_the_ID(), 'wpcf-timezone-select-test'));
return date( 'H:i', current_time( 'timestamp' ));
}

This takes a location based on the PHP timezone location list below.
https://www.php.net/manual/en/timezones.php

It should render the time at the location that is specified, however the user notices that there is Daylight saving time activated even in countries that don't observe this.

Solution:

The user had set a specific timezone in their wordpress settings to a location that observes Daylight Saving Time. This had the effect that it will offset all the clock location by the DST rules.

To workaround this I would recommend setting your wordpress settings timezone to UTC or GMT with no offsets.

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 17 replies, has 2 voices.

Last updated by Ben 5 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1224134

Ben

I have been using this function with custom code snippets and have had no problems until now.

add_shortcode('time-date', 'timedate');
function timedate() {
date_default_timezone_set( get_post_meta(get_the_ID(), 'wpcf-timezone-select-test'));
return date( 'H:i', current_time( 'timestamp' ));
}

Ever since Daylight Saving Time has started, all the times shown using this are 1 hour ahead of the actual time. I have tested and both the server and WordPress are showing the correct times so I cannot see what is causing this all of a sudden.

Can you help?

#1224183

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ben,

Thank you for contacting our support forum.

I would recommend adding some conditional logic to account for the daylight savings time since this is not something that is automatic.

Have a look at the link below.
hidden link

Please let me know if this helps.

Thanks,
Shane

#1224503

Ben

Hello Shane!

Thank you for your reply.

I thought that PHP took care of time zones when you use these (hidden link) as I am using?

#1224597

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ben,

You are correct it should set the timezone accordingly.

I tested the shortcode with an appropriate timezone and it works fine. It seems to me that the correct data isn't being passed.

Could you send me a screenshot of an example post where it is getting the data from for the timezone ?

I tried it with the manual entry of a timezone e.g America/New_York and it worked fine.

Thanks,
Shane

#1224600

Ben

Hello Shane,

I think I may be able to best explain with a few examples below.

Europe/London for London actual time is 16:50 showing 17:50
Asia/Beirut for Beirut actual time is 18:50 showing 19:50
Australia/Brisbane for Brisbane actual time is 01:50 showing 02:50

This is interesting because Brisbane doesn't use any DST.

Let me know if you still need screenshots.

#1224602

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2019-04-02 at 10.57.11 AM.png
Screenshot 2019-04-02 at 10.57.15 AM.png

Hi Ben,

When I enter it, this is what I get. See Screenshots.

Its quite possible that the server is playing a role here or the timezone settings.

You can contact your hosting service to see what timezone they are using because your shortcode functions correctly.

Thanks,
Shane

#1224612

Ben

I have spoken to them already and it works as expected for them.

Is there any way you can set up a test your side using the exact code I used above and then define the timezones using a custom field as I do and see if that affects results in any way?

Maybe it's something to do with that?

#1224726

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2019-04-02 at 3.08.38 PM.png
Screenshot 2019-04-02 at 3.07.49 PM.png
Screenshot 2019-04-02 at 3.08.05 PM.png
Screenshot 2019-04-02 at 3.08.10 PM.png

Hi Ben,

This also works fine for me.

See Screenshots

#1225199

Ben

This is very strange! I can't figure out what is going on.

#1225205

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ben,

Would you mind allowing me to have admin access to you site so that I can see what is happening from your perspective?

Send me a link to one of the pages where you are testing this.

Thanks,
Shane

#1225407

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ben,

Could you update the account to an admin account.

I would like to see the timezone setting in your wordpress account.

Thanks,
Shane

#1225411

Ben

That should be showing now.

#1225433

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ben,

I found the issue.

The problem was that your site timezone was set to london, which is fine. However it is throwing off the time measurements.

I've set it to GMT with no offsets and the correct time is being displayed now.

Thanks,
Shane

#1225456

Ben

Hello Shane,

Thank you so much for all your help with this!

With this set as GMT, does it affect all the times shown on the backend and front end? Or just the times seen when logged into the dashboard?

#1225470

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ben,

It affect the times that wordpress uses as well.

So all the times that you see on the site will be posted in the time relative to the GMT time.

If you have it at GMT right now, when you create a post it will set the time relative to GMT.

What i believe is happening is that wordpress has an internal system that has a list of timezones that observe DST and is setting the offsets based on this.

Thanks,
Shane