Clarkeez Posted August 13, 2011 Share Posted August 13, 2011 Hey.. I need some sort of function to convert the 'datetime' stamp from my mysql table row. It needs to convert that time into the users local time. I've searched around but nothing seems related to what I need. Anyone know good way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/244700-converting-time-to-users-localtime/ Share on other sites More sharing options...
Psycho Posted August 13, 2011 Share Posted August 13, 2011 Do you know the timezone for the user? If so, converting a time to another timezone is a simple task. Ideally, I would store the time from GMT, but even if you have it stored in a different timezone you can still convert from that to another. But, the real question is whether you have captured the timezone for the user, such as having them select their timezone when they create an account. If you have not provided a means for the user to select their timezone, there are ways to auto-detect it via JavaScript, but I'm not sure how foolproof that is. Quote Link to comment https://forums.phpfreaks.com/topic/244700-converting-time-to-users-localtime/#findComment-1256871 Share on other sites More sharing options...
voip03 Posted August 13, 2011 Share Posted August 13, 2011 http://php.net/manual/en/function.strftime.php http://php.net/manual/en/function.localtime.php Quote Link to comment https://forums.phpfreaks.com/topic/244700-converting-time-to-users-localtime/#findComment-1256876 Share on other sites More sharing options...
voip03 Posted August 13, 2011 Share Posted August 13, 2011 mktime http://php.net/manual/en/function.mktime.php Quote Link to comment https://forums.phpfreaks.com/topic/244700-converting-time-to-users-localtime/#findComment-1256878 Share on other sites More sharing options...
Clarkeez Posted August 13, 2011 Author Share Posted August 13, 2011 Thanks guys, I suppose I should get their timezone with the registration form. Erm, so once I have that, how would i actually do it.. something like, for example.. mysql_connect blah blah $q = mysql_query("SELECT p.time_posted AS time_posted, u.timezone AS timezone FROM post.p LEFT OUTER JOIN user u ON u.id = p.poster WHERE p.id = 'whatever'"); // time_posted field type is datetime $d = mysql_fetch_array($q); $localtime = idontknowthisbit_somefunction?($d['time_posted'], $d['timezone']); echo $localtime; Quote Link to comment https://forums.phpfreaks.com/topic/244700-converting-time-to-users-localtime/#findComment-1256906 Share on other sites More sharing options...
jcbones Posted August 13, 2011 Share Posted August 13, 2011 http://php.net/manual/en/function.date-default-timezone-set.php It should be set at the top of the script, then all date functions used in the script would be based on the set timezone. supported timezones Quote Link to comment https://forums.phpfreaks.com/topic/244700-converting-time-to-users-localtime/#findComment-1256915 Share on other sites More sharing options...
The Little Guy Posted August 13, 2011 Share Posted August 13, 2011 This Converts Pacific time to Central time: SELECT CONVERT_TZ( NOW( ) , 'US/Pacific', 'US/Central' ) ; Quote Link to comment https://forums.phpfreaks.com/topic/244700-converting-time-to-users-localtime/#findComment-1256932 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.