Jump to content

Question about how to track and display time correctly


JeremyCanada26

Recommended Posts

I'm interested to know how other people keep track of time and also how they display time correctly back to the users. I'm using a third party API that I can tie into that actually gives me the userID of a user, their full name and the timezone for that user. After checking a few users data, this is an example data of what I'm working with.

 

userID: 234213412

first_name: foo

last_name: bar

timezone: -6

 

Basically when a user visits my web app, I create a new account for them and store the above data. Currently, I'm just fetching the result from time() and converting it into MySQL DateTime format and storing that into my database.

 

//get the current time as an integer
$php_timestamp = time();
//formats the time according to MySQL DateTime type
$this->mysql_formatted_time = date('Y-m-d H:i:s', $php_timestamp);

 

I would think the current way that I'm doing this is not very good because there is a possibility that I could introduce load balancing servers into the mix and they might not be located in the same region. Also, I would have no idea how to factor the time and modify it according to the timezone value.(in my above example, -6)

 

Can anyone help me decide what method I should use to keep all of my servers timestamps in sync and how to display the time to the end user so that it looks correct to them?

Link to comment
Share on other sites

Personally, I think it best to store all times as GMT in the database. Then if you change servers (and the timezone changes) you don't have a mixed base for time data in the database.

 

To get the GMT value, use the time() function in php. In mysql you can use FROM_UNIXTIME(), or UTC_TIMESTAMP() for inserting/updating data.

 

When retrieving data use UNIX_TIMESTAMP() to convert a DATE/DATETIME column to a unix timestamp. Use date_default_timezone_set() to set the user's timezone, then use the date() function (in PHP) to format.

Link to comment
Share on other sites

Sure, here is a function that sets the timezone according to the gmt.

 

<?php 
//timezones function.
function setTimeZone($gmt) {
$times = array(
'0' => 'Europe/London',
'+1' => 'Europe/Rome',
'+2' => 'Asia/Jerusalem',
'+3' => 'Europe/Moscow',
'+4' => 'Asia/Baku',
'+5' => 'Asia/Ashgabat',
'+6' => 'Asia/Dhaka',
'+7' => 'Asia/Phnom_Penh',
'+8' => 'Asia/Hong_Kong',
'+9' => 'Asia/Seoul',
'+10' => 'Pacific/Port_Moresby',
'+11' => 'Pacific/Guadalcanal',
'+12' => 'Pacific/Fiji',
'-11' => 'Pacific/Midway',
'-10' => 'Pacific/Honolulu',
'-9' => 'America/Juneau',
'-8' => 'America/Los_Angeles',
'-7' => 'America/Denver',
'-6' => 'America/Chicago',
'-5' => 'America/New_York',
'-4' => 'America/St_Thomas',
'-3' => 'America/Buenos_Aires',
'-2' => 'Atlantic/South_Georgia',
'-1' => 'Atlantic/Cape_Verde'
);
if(!is_numeric($gmt)) { return false; }
date_default_timezone_set($times[$gmt]);
return true;
}


//usage of function
if(setTimeZone('+10')) {
echo date('m-d-Y g:i:s a');
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.