PyraX Posted September 15, 2008 Share Posted September 15, 2008 Hi, I was wondering if anyone had a script that can get the locale time of the users computer so that the server can use that instead of the "America/Denver" that it seems to keep defaulting to? Thanks PyraX Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 15, 2008 Share Posted September 15, 2008 Here is something I came up with a while ago to get the client's local date/time - <?php if (isset($_GET['offset'])) { $minutes = $_GET['offset']; echo "GMT offset (in minutes, from the browser): ". $minutes ."<br />\n"; echo "GMT: ". gmdate("Y-m-d H:i:s") ."<br />\n"; $local = gmmktime(gmdate("H"),gmdate("i")-$minutes); // adjust GMT by client's offset echo "Calculated client's date/time: ". gmdate("Y-m-d h:i:s a",$local) ."<br />\n"; } else { echo "<script language='javascript'>\n"; echo "var d = new Date();\n"; echo "location.href=\"${_SERVER['SCRIPT_NAME']}?offset=\" + d.getTimezoneOffset();\n"; echo "</script>\n"; exit(); } ?> Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/#findComment-641729 Share on other sites More sharing options...
Garethp Posted September 15, 2008 Share Posted September 15, 2008 A simple google search turned up this. How weird http://us2.php.net/localtime localtime() returns an array so try this <?php $Time = localtime(); echo "Hours:" . $Time[2] . "<br> Minute:" . $Time[1] . " <br> Second:" . $Time[0]; ?> do print_r(localtime()) to see the full array and what other information it has Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/#findComment-641730 Share on other sites More sharing options...
PFMaBiSmAd Posted September 15, 2008 Share Posted September 15, 2008 That gets the local time on the server and has nothing to do with the question that was asked. Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/#findComment-641736 Share on other sites More sharing options...
PyraX Posted September 15, 2008 Author Share Posted September 15, 2008 Thanks so much PFMaBiSmAd now just need to set the server time with the right offset. Do you know the command? Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/#findComment-641752 Share on other sites More sharing options...
PFMaBiSmAd Posted September 15, 2008 Share Posted September 15, 2008 Any script that uses date/time functions that are dependent on the time zone can set the time zone using the date_default_timezone_set() function using parameters like - 'Etc/GMT+1', 'Etc/GMT+10', 'Etc/GMT-13', ... Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/#findComment-641766 Share on other sites More sharing options...
PyraX Posted September 15, 2008 Author Share Posted September 15, 2008 It seems to be reverting to defaults on every page change here is my script <?php if (!strstr(date_default_timezone_get(),"Etc/GMT")) { echo date_default_timezone_get(); // returns America/Denver if (isset($_GET['offset'])) { $offset = ss($_GET['offset']) / 60; if ($offset >= 0) { $offset = "+".$offset; } date_default_timezone_set('Etc/GMT'.$offset); $_SESSION[TIMEOFFSET] = $offset; echo date_default_timezone_get(); // returns Etc/GMT-10 } else { echo "<script language='javascript'>\n"; echo "var d = new Date();\n"; echo "location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}&offset=\" + d.getTimezoneOffset();\n"; echo "</script>\n"; exit(); } } ?> Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/#findComment-642408 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2008 Share Posted September 16, 2008 Because the code causes a page to request itself to pass the value to the server, you should only get the value if you don't already know it (save the value in a session or a database.) Give the following a try - <?php session_start(); if(!isset($_SESSION['time_zone'])){ // if the time zone is not known, get it from the client if (isset($_GET['offset'])) { $minutes = $_GET['offset']; $hours = $minutes/60; $tz = sprintf('%+d', $hours); $_SESSION['time_zone'] = $tz; date_default_timezone_set("Etc/GMT{$_SESSION['time_zone']}"); // the following four lines are for demonstration purposes and can be removed echo "GMT offset (in minutes, from the browser): $minutes, Timezone = Etc/GMT$tz<br />\n"; echo "GMT: ". gmdate("Y-m-d H:i:s") ."<br />\n"; $local = gmmktime(gmdate("H"),gmdate("i")-$minutes); // adjust GMT by client's offset echo "Calculated client's date/time: ". gmdate("Y-m-d h:i:s a",$local) ."<br />\n"; } else { echo "<script language='javascript'>\n"; echo "var d = new Date();\n"; echo "location.href=\"${_SERVER['SCRIPT_NAME']}?offset=\" + d.getTimezoneOffset();\n"; echo "</script>\n"; exit(); } } else { // this visitor's time zone is already known, set the default timezone for all date/time calculations date_default_timezone_set("Etc/GMT{$_SESSION['time_zone']}"); } echo "Server time by setting timezone: " . date("h:i:s a"); ?> Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/#findComment-642463 Share on other sites More sharing options...
PyraX Posted September 16, 2008 Author Share Posted September 16, 2008 Works perfectly, Thank you so much Link to comment https://forums.phpfreaks.com/topic/124274-setting-locale-time/#findComment-643400 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.