Skip Navigation

[Resolved] Date time with timezone

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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 3 voices.

Last updated by Luo Yang 2 years ago.

Assisted by: Luo Yang.

Author
Posts
#2593633

Hi there,

My apologies if this was asked before but I can only find a slightly related post from 4 years ago.

As many people I am creating an events feature for our new website. An admin can set a start and end DateTime using the Date custom field.

We are based in Belgium so we are in Central Europe time but the timestamp saved in the database seems to be UTC. I can not request from our admins to convert the start and end time for their events to UTC first so basically all timestamps in the database will be wrong. Not a big an issue if we just wanted to convert the timestamp to a formatted date with time but I expect this to become a problem when I want to convert the timestamp to other time zones.

For example, we also organise webinars. Later on, I would like the dates and times on the front end for these webinars to be show to the user in their own time. This might become problematic since the timestamps are UTC in de database but the admins entered Central Europe local time.

An other possible issue might occur for event we organise in other timezones where local start and end dateTimes will be added by the admins.

I am a bit surprised the date field does not have a timezone selection. So I am wondering what the best solution would be.

- Expect from our admins to convert every start and end time to UTC first and use that in the date fields. Then convert the time to be shown on the front end to the local time of the event in case of a live event and to the local time of the user in case of an online event.

- Ignore that the timestamp in the database is UTC and assume it is always in Central European time. Which will probably become problematic for events in other timezones.

- A solution I'm not thinking off right now.

Thanks!

#2593699

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Kim

Unfortunately there is no getting round the fact that our implementation of this is not good.

The original developers of Types (more than a decade ago) didn't implement it correctly, and by the time it began to be questioned years later, it was difficult to come up with a solution for new users that didn't potentially break things for existing users, and we are still in the same position.

So let me explain how it works compared to how it should, so that you can at least make informed choices about how you want to work with this.

Types date fields are stored as "timestamps" (i.e. the number of seconds since 1 Jan 1970).

I say "timestamps" because they are not true timestamps.

True timestamps are—by definition—UTC.

Imagine the timezone setting of a site is New York (currently UTC-4).

Someone enters a post with some date on that site. When that content is stored in the database, the date/time should first be converted to UTC, and then that date/time is converted to a timestamp, which gets stored in the database.

When it comes to display this in a user-friendly date/time format, the timestamp should first be converted from UTC to the correct timezone, then output in the desired format.

But Types doesn't convert to UTC before generating the timestamp for storage (nor "unconvert" from UTC when it comes to displaying it), it just creates a "timestamp" as if it were UTC already.

Note that means when dates are entered (when publishing/editing a post, for example) it is always in the site timezone.

In that sense it doesn't matter that Types doesn't convert the dates to UTC before generating the timestamps (and reverse this when displaying the field), if you put 14 April 16:00 in you will get 14 April 16:00 out, unless you modify the site's timezone setting.

And even though the site timezone might be set to NY, if all of the users were in Spain, say, then it also wouldn't matter.

It can matter when you have users and content across different timezones, but it might not.

If you have a concert that is at 21:00 in NY you would likely want to show site visitors that the concert was at 21:00 regardless of where they were visiting from.

But if you had a webinar that was at 11:00 in NY you would want a site visitor based in London to see that it was at 16:00.

So you may need to customise how the field is output depending on your needs (e.g. creating a shortcode to output the date/time reflecting someone's timezone, allowing for the fact that the "timestamp" is not UTC but is in fact based on the timezone of the site itself).

I hope that at least clarifies how the field works so you can be confident when it comes to handling it.

#2595099

Yeah, I thought that's was how it supposed to work. Thanks for you honesty. What a bummer. I really like Toolset.

I really have to think about user over different timezones. Is there a way I can manipulate the fields output before it is entered in the database? So I can create a separate field for the admin to set the timezone and have any input converted to UTC before it is written in the database.

Maybe I'm not thinking about all the factors but since the field now allways writes UTC timestamps in the database. Isn't it just a matter of adding a timezone field and setting it to UTC by default. It should not change the existing timestamps for current users.

Alternatively, why don't you create a correct DateTime field with timezone, alongside the existing one? So at least users who need it have one.

#2595147

Hello,

The Toolset custom date field stores value in timestamp format, and display the date field value using timezone of your website setting:
hidden link

So when user input the value into custom date field, it is using the same timezone of your WordPress website setting.

By default, WordPress does not support different time zone settings for each user, so there isn't such kind of built-in feature within Toolset plugins.

In your case, you might consider custom codes, for example:
1) Add a timezone select field after the custom date field
2) After user fill and submit the form, use action hook "save_post" to trigger a custom PHP function:
https://developer.wordpress.org/reference/hooks/save_post/
Get the custom date field and timezone field value:
https://developer.wordpress.org/reference/functions/get_post_meta/
And change the values to what you want:
https://developer.wordpress.org/reference/functions/update_post_meta/