2

As working on a large application I am trying to make the datetime stamps reconcile with the current user time. so If activity id done at 3:00PM then the every user see it at 3:00PM

So Here is solution steps to the problem. on this and please correct me or lead me to the right direction if I am not on the right direction.

  1. Store all datetime in MySql as UTC time.

  2. Store time zones in a table with the current offset. for example

    zone_id  zone_name  utc-offset
    1        Central    -6
    
  3. In my users table I have a field for user_time_zone_id and in that field I will have the value "1" in the user setting so it will say that this user is using the system from "Central" location.

  4. In my php application configuration I set the default time zone to UTC like this

    date_default_timezone_set('UTC');
    
  5. Once I load this application I define the user offset and on each datetime out put I do the calculation of the time. for example date('Y-m-d', strtotime($current_offset.' hour')) where $current_offset = -6 as it is define by the user profile upon the page load.

Here are my questions.

Is my approach to this problem correct? Is there a better way of doing this? How to calculate the daylight saving time? Please keep in mind that there are some parts of the country that does not have daylight saving.

fthiella
  • 48,073
  • 15
  • 90
  • 106
Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • 1
    Don't do the offsets manually. Use date_default_timezone_set() to set the application to the user's timezone. There are [timezone idiosyncrasies](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result/6841479#6841479) that you shouldn't waste your time accounting for when PHP will automatically. – Mike B Mar 21 '13 at 15:35
  • Oh, that's a good idea. so Set the page to the current user setting and then when the user load a page they everything will be converted to his/her current datetime. But wait what happens when they want to edit a datetime field? – Jaylen Mar 21 '13 at 15:59
  • I don't recommend using `date_default_timezone_set` for this, it will likely lead to problems and confusion when the dates in your DB are in a different timezone than you've put PHP into. It can also mess with unexpected things like, for example, timestamps in logfile output. – Adrian Mar 21 '13 at 16:00
  • @Adrian Use a timezone-free context when saving date/times to the db. i.e. unix timestamps - or store everything in UTC datetime fields as OP already says he's doing. – Mike B Mar 21 '13 at 16:03
  • http://stackoverflow.com/questions/2505681/timezone-conversion-in-php/2505687#2505687, http://stackoverflow.com/questions/5768380/php-mysql-and-time-zones, http://stackoverflow.com/questions/346770/dealing-with-timezones-in-php, http://stackoverflow.com/questions/518296/best-way-to-handle-storing-displaying-dates-in-different-timezones-in-php, http://stackoverflow.com/questions/5559103/php-date-default-timezone-set-eastern-standard-time-est/5559239#5559239 – Mike B Mar 21 '13 at 16:08

2 Answers2

1

I had a similar thing one time, and it ended up being a pain to try to keep track of users timezones and daylight savings, especially when half your clients are in AZ which doesn't have daylight savings. I don't know if this is possible for you, but what I ended up doing was just store everything in UTC and then used JS to convert it to the users local time with the Date object. It was done through an ajax call, but you could also echo a document.write if you needed to.

romo
  • 1,990
  • 11
  • 10
  • PHP handles Arizona, Indiana, and all the other funky offsets just fine if you set the timezone properly. – Mike B Mar 21 '13 at 16:05
0

You shouldn't need to use date_default_timezone_set, use the PHP DateTime class which has native support for timezones. Or, like @romo says, you can do it on the client in JavaScript.

Adrian
  • 42,911
  • 6
  • 107
  • 99
  • How can I user the PHP DateTime class for this? I am not familiar with that class and never used it. Can you please elaborate on how to user it to solve my problem? Thanks you – Jaylen Mar 21 '13 at 16:08
  • In my original post it's linked to the PHP manual page for the class, it explains how to use it. You intialize it with your date and call `setTimezone`, and it's automatically shifted to the specified timezone. You can then get your shifted date back out or format it directly using the class `format` method instead of needing to use `date()`. – Adrian Mar 21 '13 at 16:11
  • Okay, So I am trying to follow the example and I am not getting any time changes here is what I have done so far $user_time_zone = new DateTimeZone('Europe/Warsaw'); $schedule_date = new DateTime($call['triggerOn'] , $user_time_zone ); echo $schedule_date->format('Y-m-d h:i A'). The time is not beeing converted. am I following your idea? – Jaylen Mar 21 '13 at 16:47
  • Adrian, this is one output from $call['triggerOn'] 2013-02-27 18:00:37 – Jaylen Mar 22 '13 at 00:12
  • If that's in UTC, you'll need to pass UTC as the TimeZone in the constructor (it's the time zone it uses to interpret the time passed to the constructor.) Then you can call `setTimezone` on the object you've just created to convert it to your user's time zone. Basically, you have to specify the "from" and "to" timezones for your conversion: "from" in the constructor, then "to" in a method call on the resulting object. – Adrian Mar 25 '13 at 16:46