views:

2103

answers:

6

Hello all,

I have just realised if I add a particular record to my MySQL database - it will have a date/time of the server and not the particular user and where they are located which means my search function by date is useless! As they will not be able to search by when they have added it in their timezone rather when it was added in the servers timezone.

Is there a way in Codeigniter to globally set time and date specific to a users location (maybe using their IP) and every time I call date() or time() that users timezone is used.

What I am actually asking for is probably how to make my application dependent on each users timezone?

Maybe its better to store each users timezone in their profile and have a standard time (servers time) and then convert the time to for each user?

Thanks all

+1  A: 

I think the easiest way is define a timezone for internal data storage (your server's time zone or UTC) and convert the time according to the user's time zone when outputting it.

I don't know CodeIgniter so I can't point you to the right functions. One prominent library that is timezone aware is Zend_Date: Working with Timezones I have not worked with these functions yet but they look promising.

However, once you know the user's time zone, it's not difficult to put together an own function that adds/substracts the offset when outputting dates and/or times.

Possibly related question:

Pekka
you're first by seconds, argh! :]
Adam Kiss
Thanks Pekka :)
Abs
+1  A: 

I think recalculating to user's time is better option, since it gives you normalized time on server, i.e. if you'll need to look up something, that happened (from your point of view) hour ago, you won't have a mess with american, asian and e.g. australian time.

Just ask them for their timezone (usually select with major cities in that timezone) and then recalculate :)

Or, alternatively, you can store two timedates - one for your comparison and one to show, so you won't have so much calculations on serverside.

Also, if recalculating, you can use date helper: http://codeigniter.com/user_guide/helpers/date_helper.html

Adam Kiss
+1  A: 

It sounds like what you need to do is store all of the date and times in your system as UTC time (used to be called GMT). This is the base time that everything in the world is calculated off of with adjustments. (eg: Central Time is -6 hours off of UTC)

In MySQL you can use UTC_TIMESTAMP() to get the current UTC time as long as your server and DB are configured with the correct times and timezone settings.

In PHP run this to set the timestamp of PHP to UTC (you will run this in your code so put it on every page or in a centralized index file):

date_default_timezone_set('UTC');

Or you can go directly into PHP.INI and tell it to use UTC time globally. (this may not work if you have multiple websites on a single installation of PHP.

And then anywhere in the system you need to get the current UTC time you can just call:

time();

Then for each user in the system you will need to ask them what timezone they live in and then when you display times make the adjustment for that user. So if it is 5:00PM UTC and I live in Easter US (-5) the time would be 5:00 - 5 hours = 12:00PM.

This can be a long process to get right but once you do your users will find it very useful especially internationally.

angryCodeMonkey
A: 
Donny Kurnia
A: 
ChazUK
A: 

I've used the MySQL built-in timezone conversion. In the database, all datetimes are stored as UTC. In the select query, I used CONVERT_TZ to convert to the user's timezone. You can specify timezone codes or hour invervals like:

SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');

But, the problem is this does not accommodate for daylight savings times. This is particularly frustrating since many parts of the world either don't honor daylight savings or honor it on different dates. So, if you install the timezone description tables, you can use descriptive names that will account for daylight savings automatically like:

SELECT CONVERT_TZ('2004-01-01 12:00:00', 'UTC', 'US/Eastern');
spoulson