Welcome Guest, Not a member yet? Register   Sign In
Localised timezones?
#1

[eluser]Phil Sturgeon[/eluser]
Using CI how can one go about forcing all times used on a server to be stored in one timezone (GMT), then whenever data is displayed (using CI date helpers) to be shown in the current users current timezone?

This is a bloody confusing matter for me, someone spoonfeed me? Smile
#2

[eluser]xwero[/eluser]
In the config file you set
Code:
$config['time_reference'] = 'gmt';
Then using the date helper you can use now() to add the time to the database and using gmt_to_local() you can show the date for the timezone the user choose/ is fetched from the browser.

Now you can burp Smile
#3

[eluser]Phil Sturgeon[/eluser]
I do this too often, ask for help then work it out afterwards.

In date helper:
Code:
function local_date($format = '', $unix_time = 0)
{
    $timezone = timezones(getUserProperty('timezone'));
    
    if($timezone != 'UTC' && !empty($timezone)):
        $unix_time += $timezone * 3600;
    endif;
    
    return date($format, $unix_time);
}

Tastey! ^_^
#4

[eluser]Michael Wales[/eluser]
Wish I would have seen this one before you figured it out pyro. People always give me a hard time when I tell them to always store date/time as a Unix timestamp (rather than a MySQL Date/Time field). This is exactly why - you see how beautiful and functional that function is? 5 lines - and it works exactly as you would expect it to.

Plus, Unix timestamps just make more sense, and damn near all of the PHP Date/Time functions use a Unix timestamp for their calculations. Why insert a MySQL date/time field when you are going to convert it to Unix everytime you retrieve it?
#5

[eluser]xwero[/eluser]
unix timestamps are good within the brief time they are usable (from 1970 to ... with a quirk 19-01-2038). The mysql date has a bigger reach (1000-01-01 to 9999-12-31) when you store it as a number.

I have had a few sites where they wanted to archive their first data that was before 1970 so i had to write functions based on the mysql date.
#6

[eluser]Michael Wales[/eluser]
Good point - I guess being the "young buck" I am I have had no reason to involve myself with anything 1970.

Hell, with deployments the way they are going I may not have to involve myself with 2038 either - lol.
#7

[eluser]xwero[/eluser]
Are we feeling a bit selfish or are you just providing work for future generations Smile
#8

[eluser]Michael Wales[/eluser]
Deployments as in war deployment - not application deployments.

There is plenty of work to go around for us all. Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB