JeremyCanada26 Posted November 12, 2010 Share Posted November 12, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/218527-question-about-how-to-track-and-display-time-correctly/ Share on other sites More sharing options...
harristweed Posted November 13, 2010 Share Posted November 13, 2010 Why not use GMT? Quote Link to comment https://forums.phpfreaks.com/topic/218527-question-about-how-to-track-and-display-time-correctly/#findComment-1133699 Share on other sites More sharing options...
JeremyCanada26 Posted November 14, 2010 Author Share Posted November 14, 2010 sry, what exactly do you mean? how do I do it? Quote Link to comment https://forums.phpfreaks.com/topic/218527-question-about-how-to-track-and-display-time-correctly/#findComment-1133978 Share on other sites More sharing options...
DavidAM Posted November 14, 2010 Share Posted November 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218527-question-about-how-to-track-and-display-time-correctly/#findComment-1134002 Share on other sites More sharing options...
JeremyCanada26 Posted November 14, 2010 Author Share Posted November 14, 2010 Is there any way for me to do this using the -6 or -5 value that I already have along with the users data? Ricky Quote Link to comment https://forums.phpfreaks.com/topic/218527-question-about-how-to-track-and-display-time-correctly/#findComment-1134057 Share on other sites More sharing options...
JeremyCanada26 Posted November 15, 2010 Author Share Posted November 15, 2010 i still haven't figured out a good solution on how to factor in the value of -5 or -6 into the mix yet Quote Link to comment https://forums.phpfreaks.com/topic/218527-question-about-how-to-track-and-display-time-correctly/#findComment-1134418 Share on other sites More sharing options...
JeremyCanada26 Posted November 17, 2010 Author Share Posted November 17, 2010 Can anyone provide an example for usage? Quote Link to comment https://forums.phpfreaks.com/topic/218527-question-about-how-to-track-and-display-time-correctly/#findComment-1135283 Share on other sites More sharing options...
jcbones Posted November 17, 2010 Share Posted November 17, 2010 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'); } Quote Link to comment https://forums.phpfreaks.com/topic/218527-question-about-how-to-track-and-display-time-correctly/#findComment-1135308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.