PHP puzzle

I have a general question about working with daylight saving time. I assume that this is not very specific PHP, but I write in PHP, so I believe that this will not hurt to include it.

I have a calendar application built using jquery fullcalendar. A user views events in their local time zone, and my server stores them as UTC time in mysql. (Other questions about stackoverflow suggest that this is the best way to deal with time zones.) Thus, a conversion occurs each time a user saves or views events on a calendar. It works great, but I'm confused about how best to deal with summer time.

For example, let's say a user in the EST time zone (Eastern Standard Time) creates an event when it is not a summer day for 3 hours, which is repeated every day. My PHP code uses the standard DateTime class (and DateTimeZone) to convert between UTC and EST to UTC-5: 00. When daylight saving time is active, the clock rotates one hour, and instead, PHP will convert between UTC and EST by UTC-4: 00. From the point of view of the user, the event then shifts from the original 3 pm to 4 pm. This is not what my users and I want. The 3pm event should remain at 3pm, regardless of daylight saving time. What is the best way to handle this? Is there a way in PHP to ignore daylight saving time?

My code is:

$getDate = new DateTime($storedDate, new DateTimeZone('UTC')); $getDate->setTimezone(new DateTimeZone('America/New_York')); $getDateString = $getDate->format('Ymd H:i:s'); 

FURTHER INFORMATION: (copied from my comments below)

- The first occurrence of a recurring event is saved one time. All other events are created "on the fly" in accordance with a calendar view of user requests (month, week or day). With the way I encoded it, it only creates entries that will be visible in the view.

“The thing is, I also need to keep it constant for other time zones.” To continue the original example, 3 pm should remain for 3 hours in EST (regardless of daylight saving time), but if the event is viewed at central time, the event should also remain for 2 hours (regardless of daylight saving).

Essentially, I need to somehow ignore daylight saving time.

+7
php datetime dst
source share
2 answers

Have you tried looking at DateTimeZone :: getTransitions ()?

http://www.php.net/manual/en/datetimezone.gettransitions.php

In particular, use the [offset] and [isdst] properties.

  • When they save time, find the first transition to the current date is NOT DST . (Typically, this is one of two values ​​over the past year). Convert Using Non-DST Period Offsets
  • When retrieving a value and currently in a DST period, use a period offset other than DST to translate the time, not the current offset.

Taking your EST example, in August, even if you are in an EDT, you save the values ​​using the EST -5 transform.

When you pull the value back, if they see this value in January, you add 5, and if you are in August, you add 4.

This will work in 95% of cases. I assume the switches are consistent. If Eastern decided to merge with Central, you may have transitions from -5 / -4 / -5 / -4 / -5 / -5 / -6 / -5 / -6 / -6 and this will ruin everything.

There is no magic bullet for this. I don’t know the details of the structure of your application, you just need to try adding 3 hours before midnight of any day when you continue, so that the daily daily meeting is saved as soon as time .

+3
source share

This is a difficult problem. I learned that not every day is 86,400 seconds.

When I was working on a calendar application, I made a decision at an early stage, which saved me a lot of trouble. That is, each instance of the event has a record in the database.

One table was intended for all information about the event (name, description, etc.). In another table, there was a timestamp for each instance of this event. When someone scheduled a repeat event (for example, 3:00 PM every Wednesday), I would insert an instance at 3:00 PM in my time zone (which is actually stored as UTC) every Wednesday. Now events in theory can be repeated forever. I decided that it was much easier to set a reasonable limit (say, 50 or 100 years) for repetition than it would be necessary to calculate all the dates of the event on the fly. To the user, it looks like the event lasts forever, but in the database it is not. Even if you have planned an event every day for 100 years, there are only 36,500 entries in a very narrow table.

With this approach, you should consider exceptions. Sometimes people change the details of a single instance of an event. In these cases, I simply created another event and copied the relevant data ... since this is actually a separate event. If you wanted to link them all together with a group identifier, you could. Changing individual event planning is easy because there is a separate line for each instance.

I recommend this approach for most scenarios like this. This saved a lot of hassle so that I could rely on a database for all the heavy lifting, and at the same time solve the problem with the time zone.

+2
source share

All Articles