Time on clients computer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Time on clients computer

Post by newmember »

Is there some way to know what is a current local time on user's computer?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

use javascript's date time functions for this purpose.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

i need to know the time on server side
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

use localtime() function for this...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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) ...
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

you need to pass time information from the client
how??

can use clients ip and request time information from some time server?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no. It requires using Javascript or some other client-side scripting to retrieve it's time information.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);

?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I am sure there is a PHP function within OSCommerce which does exactly what you require.


Joe 8)
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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>
Post Reply