Jump to content

Setting locale time


PyraX

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.