Page 1 of 1

Time on clients computer

Posted: Fri Feb 25, 2005 6:30 am
by newmember
Is there some way to know what is a current local time on user's computer?

Posted: Fri Feb 25, 2005 7:20 am
by n00b Saibot
use javascript's date time functions for this purpose.

Posted: Fri Feb 25, 2005 7:27 am
by newmember
i need to know the time on server side

Posted: Fri Feb 25, 2005 7:39 am
by n00b Saibot
use localtime() function for this...

Posted: Fri Feb 25, 2005 8:08 am
by feyd
you need to pass time information from the client, there's no guaranteed way otherwise. Javascript has a time function that'll write the timezone offset of the computer (in seconds) ...

Posted: Fri Feb 25, 2005 1:21 pm
by newmember
you need to pass time information from the client
how??

can use clients ip and request time information from some time server?

Posted: Fri Feb 25, 2005 4:43 pm
by feyd
no. It requires using Javascript or some other client-side scripting to retrieve it's time information.

Posted: Fri Feb 25, 2005 5:22 pm
by s.dot
Have the server's hour stored in a variable, and allow the user to choose their time offset from the server. For example they're 3 hours behind the servers time.

Code: Select all

$offset = "-3"; // This would come from the users input, could be + or -

$hour = date("g:i A");
$usertime = $hour$useroffset
echo $usertime;
This would print userhour:minute AM/PM

Posted: Sat Feb 26, 2005 11:24 am
by newmember
ok,i understand.

but i'm a little :? how javascript can help me to know time on user computer?
what i'm trying to do is:

before the page is sent to user, i want to do some proccessing based on the time in user's timezone and only then serve appropriate content...

Posted: Sat Feb 26, 2005 12:46 pm
by feyd
it requires an intermediate page to alert your system what timezone they are in, with support for non-script enabled browsers or those that wish to bypass timezone detection.

tested example

Code: Select all

<?php

//	stolen from a timvw post
function getURL()
&#123;
	$ports = array('https' => 443, 'http' => 80);
	$prefix = empty($_SERVER&#1111;'HTTPS']) ? 'http' : 'https';
	$url = $prefix;
	$url .= $_SERVER&#1111;'SERVER_PORT'] != $ports&#1111;$prefix] ? ':' . $_SERVER&#1111;'SERVER_PORT'] : '';
	$url .= '://';
	$url .= $_SERVER&#1111;'HTTP_HOST'];
	$url .= $_SERVER&#1111;'REQUEST_URI'];
	return $url;
&#125;

session_start();

if(!isset($_GET&#1111;'!']) && !isset($_SESSION&#1111;'timezone']))
&#123;
	if(!isset($_POST&#1111;'t']))
	&#123;
		//	add your html layout here for timezone choosing display, or a template system thing...
		$url = getURL();
		$skipurl = $url . (strpos($url, '?') === false ? '?!=1' : '&!=1');
		$output = <<<STOP
<html>
	<head>
		<title>&#123;$_SERVER&#1111;'HTTP_HOST']&#125; :: Timezone Information</title>
	</head>
	<body>
		<form name="tz-detect" action="&#123;$url&#125;" method="post">
			<script language="Javascript">
				//	create a date object.
				var d = new Date();
				//	timezone offset information is returned in difference of minutes from GMT/UTC
				var t = -d.getTimezoneOffset() / 60.0;
				//	write out the detected timezone
				document.writeln('Client-side scripting enabled. Attempting to auto-detect your timezone settings... Done.<input type="hidden" name="t" value="' + t + '" /><br />This page should automatically refresh in a few seconds. If it does not, or you do not wish to wait, you may click submit below.<br />');
				//	automatically submit the form in 1 second.
				setTimeout('document.forms&#1111;''tz-detect''].submit()', 5000);
			</script>
			<noscript>Client-size scripting disabled.<br />Please select your timezone:<br /><select name="t">
				<option value="-12">GMT - 12 Hours</option>
				<option value="-11">GMT - 11 Hours</option>
				<option value="-10">GMT - 10 Hours</option>
				<option value="-9">GMT - 9 Hours</option>
				<option value="-8">GMT - 8 Hours</option>
				<option value="-7">GMT - 7 Hours</option>
				<option value="-6">GMT - 6 Hours</option>
				<option value="-5">GMT - 5 Hours</option>
				<option value="-4">GMT - 4 Hours</option>
				<option value="-3.5">GMT - 3.5 Hours</option>
				<option value="-3">GMT - 3 Hours</option>
				<option value="-2">GMT - 2 Hours</option>
				<option value="-1">GMT - 1 Hours</option>
				<option value="0" selected="selected">GMT</option>
				<option value="1">GMT + 1 Hour</option>
				<option value="2">GMT + 2 Hours</option>
				<option value="3">GMT + 3 Hours</option>
				<option value="3.5">GMT + 3.5 Hours</option>
				<option value="4">GMT + 4 Hours</option>
				<option value="4.5">GMT + 4.5 Hours</option>
				<option value="5">GMT + 5 Hours</option>
				<option value="5.5">GMT + 5.5 Hours</option>
				<option value="6">GMT + 6 Hours</option>
				<option value="6.5">GMT + 6.5 Hours</option>
				<option value="7">GMT + 7 Hours</option>
				<option value="8">GMT + 8 Hours</option>
				<option value="9">GMT + 9 Hours</option>
				<option value="9.5">GMT + 9.5 Hours</option>
				<option value="10">GMT + 10 Hours</option>
				<option value="11">GMT + 11 Hours</option>
				<option value="12">GMT + 12 Hours</option>
				<option value="13">GMT + 13 Hours</option>
			</select></noscript>
			<input type="submit" name="s" value="Set Timezone" />
		</form>
		<br /><br /><a href="&#123;$skipurl&#125;">Skip setting a timezone</a>
	</body>
</html>
STOP;
		exit($output);
	&#125;
	else
	&#123;
		$_SESSION&#1111;'timezone'] = floatval($_POST&#1111;'t']);
	&#125;
&#125;


echo 'Your timezone is now set to: GMT' . ($_SESSION&#1111;'timezone'] >= 0 ? '+' : '') . var_export($_SESSION&#1111;'timezone'],true);

?>

Posted: Sat Feb 26, 2005 2:00 pm
by Joe
I am sure there is a PHP function within OSCommerce which does exactly what you require.


Joe 8)

Posted: Sat Feb 26, 2005 5:31 pm
by newmember
if it is so complicated then i will change my idea...
the only thing i wanted it for is to change stylesheet according to time in user's timezone...
e.g. if user comes to my page at night hours (according to his time) then i would serve him stylesheet with appropriate attributes...
and if he comes at morning (his clock) then i'll serve different stylesheet
(like sunrise)

it is not really vital :) part of my site .... i just was sure it is not so complicated...
i hoped that maybe server tracks the time of request or maybe that information comes with http header that user's browser sends...

thanks anyway... (at least i know it is not trivial)

Posted: Sat Feb 26, 2005 5:57 pm
by feyd
.. actually it is trivial ..... The script I posted is an entire page for handling it, and allowing someone to set it on their own. Once it's set for the user, you can save it off in their settings.. allowing them to tweak it anytime they like, or hitting the autodetect page to update the information.

Posted: Sat Feb 26, 2005 6:43 pm
by timvw

Code: Select all

<html>
<head>
<title>el magnifico doing am/pm :)</title>
<link id="foo" rel="stylesheet" type="text/css" href="am.css"/>
<script type="text/javascript">
function gimmick(id)
&#123;
	var stylesheet = new Array();
	stylesheet&#1111;0] = "am.css";
	stylesheet&#1111;1] = "pm.css";

	var now = new Date();
	var hour = now.getHours();
	if (hour > 12)
	&#123;
  		document.getElementById(id).href = stylesheet&#1111;0];
	&#125;
	else
	&#123;
  		document.getElementById(id).href = stylesheet&#1111;1];
	&#125;
&#125;
</script>
</head>
<body onload='javascript:gimmick("foo")'>
hihi
</body>
</html>