CodeIgniter Forums
timezone help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: timezone help (/thread-49041.html)

Pages: 1 2


timezone help - El Forum - 02-06-2012

[eluser]UnknownPlayer[/eluser]
Hi,
i found this code in config.php:
Code:
$config['time_reference'] = 'local';
and i wonna know, how this work, becouse when i write time() in db, how that would be saved, on which timezone ?
And when echo that to users in different timezones ?

Thanks.


timezone help - El Forum - 02-06-2012

[eluser]Aken[/eluser]
That config item is utilized in Date helper functions and the Session library. If you're interested in displaying dates differently based on the visitor's time zone, you should save all dates in GMT. That would mean not using your database's built-in time functions (like NOW()), as those will use the database's local time.


timezone help - El Forum - 02-06-2012

[eluser]UnknownPlayer[/eluser]
So, you suggest to use $config['time_reference'] = 'gmt' or another way ?

When i set set GMT, and when insert in db, that will be ok, but when user is registred and he set GMT +1 as his default, now when he insert something in db(example: comment) it will be GMT +1, do i need to reset on every db insert ?



timezone help - El Forum - 02-06-2012

[eluser]InsiteFX[/eluser]
First off you need to set the correct time of where your server is!
I add this to the top of index.php for my servers time!
Code:
/**
*  set the default timezone.
*/
date_default_timezone_set('America/New_York');

Now all time caculations will be correct for the servers time.

If the time is not correct sessions and cookies will not work correct!

Then use this if you are allowing users to set their timezones to gmt.
Code:
$config['time_reference'] = 'gmt';

CodeIgniter User Guide - Date Helper now()

Also look at ./system/helpers/date_helper.php



timezone help - El Forum - 02-06-2012

[eluser]UnknownPlayer[/eluser]
User will have to choose timezone on his profile.

Now what should be changed for each user.

Example:
- user1 | timezone (America/New_York)
- user2 | timezone (Europe/Belgrade)
- user3 | timezone (Asia/Baghdad)

this data will be saved in mysql ofc, now when user login, what should be loaded every page to make date to his timezone ?
But when he insert something, to make GMT like Aken said that is best for mysql insert.


timezone help - El Forum - 02-06-2012

[eluser]InsiteFX[/eluser]
You need to allow them to set their timezone using gmt time.
It has to be an offset to your gmt time. Do a search on gmt time...



timezone help - El Forum - 02-06-2012

[eluser]UnknownPlayer[/eluser]
I tried like this, but not showing correct time..
btw i tried this on my localhost, and i am in Serbia.
Code:
date_default_timezone_set('America/New_York');
// time() will return new york time, i will use this to store every time and date in db  
$time = now();
$timezone = 'UP1'; // serbia timezone utc + 1
$daylight_saving = TRUE;  
$local_time = unix_to_human(gmt_to_local($time, $timezone, $daylight_saving));  
echo $local_time;

I just set on $config['time_reference'] = 'gmt'.

Now it shows me 1 hour more than it should be..

What is problem ?

EDIT:
I removed daylight saving from gmt_to_local and it works, should it be ok like that ?

Now i will store users time zone at some variable and use it like this.. i think that is the way..


timezone help - El Forum - 02-06-2012

[eluser]InsiteFX[/eluser]
You need to read this!

date_default_timezone_set



timezone help - El Forum - 02-07-2012

[eluser]UnknownPlayer[/eluser]
But this method is working nice ?


timezone help - El Forum - 02-07-2012

[eluser]Bhashkar Yadav[/eluser]
it can be done from .htaccess also by
Code:
php_value date.timezone 'YOUR TIME ZONE'


it would work nicely ....