Borderless.Nomad
Borderless.Nomad

Reputation: 761

PHP Timezone Offset in MongoDB

I am developing a system to wish birthdays using messages on a single board Using MongoDB and PHP.

Now the real problem is when system generated the message using CRON Job everyday and checks if there is any birthday or not if yes then publish it system wide. Also each user has specified their default timezones as well as messages are generated using System time which is currently +5.30 (IST).

So, suppose system have generated one message on 3rd December at 12AM the timestamp stored in DB would be as per system's Timezone. and when user visits the thing it shows as per same but I need to show it as per user's settings.

Please see attached screenshot. Datetime Problem

Upvotes: 3

Views: 1082

Answers (2)

mnemosyn
mnemosyn

Reputation: 46311

Your server code should use UTC so you don't need to care about the position of your server. Using local time can lead to issues because some points in time don't exist, or exist twice (DST jumps). However, when your server code acts on behalf of a user, it must honor that user's time zone settings, so you should look that up first. It depends on your exact requirements, however.

I'd suggest you take a look at Joda Time for the problems and solutions related to timezones. It's a Java library, but maybe there's a PHP equivalent or you can learn from their sources.

Now, your requirements: You need to make sure what exactly you want to achieve. Example: A user from LA has a friend in Australia. When do you notify the LA user about the Australian's birthday given the huge time difference? What to do about people born on the 29th of February? What if the LA user is currently in Tokyo? I'd say make your life easy at this stage, because precision and timely notification will make the problem grow surprisingly complicated.

You'd probably need a structure in MongoDB to store the birthdays, because a birthday is a recurring event rather than a point in time. You could store the next notification time in UTC, but it's not that simple - keep reading.

Also, you need to store the time zone for each user. Those time zones are annoying as well if you need precision: Their offsets can change, and their DST rules will change (sometimes on short notice). Therefore, it makes sense to store only a TimezoneId. This is the reason for the fact that denormalizing the 'next notification time' a year in advance can be a challenge.

Your exact requirements will shape the code and the data structure. My advice is: view birthdays as recurring, long-running, local-time events. Store the time zone id and the date (no time!). Your birthdays are not 'attached' to a timezone because it doesn't make sense - the birthday 'follows you' when you move to a different time zone.

It does make sense to store the next notification date and time (UTC) in the database, so your notification lookup code is easy, but keep in mind that you'll have to update these if the user changes his timezone or the user's government suddenly decides to introduce a DST rule.

As a side note, your cron should run more often than daily, at least hourly I'd say.

Upvotes: 1

Sean H Jenkins
Sean H Jenkins

Reputation: 1780

One way is to use the servers time when setting the date and time. At the moment it seems like your setting by the Users PC and displaying by the server time. Which I assume is only possible with AJAX?

Set and display with the server time only and give users choice to set their timezone.

Upvotes: 0

Related Questions