Time on clients computer
Moderator: General Moderators
Time on clients computer
Is there some way to know what is a current local time on user's computer?
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
use localtime() function for this...
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.
This would print userhour:minute AM/PM
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;- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
tested example
Code: Select all
<?php
// stolen from a timvw post
function getURL()
{
$ports = array('https' => 443, 'http' => 80);
$prefix = empty($_SERVERї'HTTPS']) ? 'http' : 'https';
$url = $prefix;
$url .= $_SERVERї'SERVER_PORT'] != $portsї$prefix] ? ':' . $_SERVERї'SERVER_PORT'] : '';
$url .= '://';
$url .= $_SERVERї'HTTP_HOST'];
$url .= $_SERVERї'REQUEST_URI'];
return $url;
}
session_start();
if(!isset($_GETї'!']) && !isset($_SESSIONї'timezone']))
{
if(!isset($_POSTї't']))
{
// 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>{$_SERVERї'HTTP_HOST']} :: Timezone Information</title>
</head>
<body>
<form name="tz-detect" action="{$url}" 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ї''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="{$skipurl}">Skip setting a timezone</a>
</body>
</html>
STOP;
exit($output);
}
else
{
$_SESSIONї'timezone'] = floatval($_POSTї't']);
}
}
echo 'Your timezone is now set to: GMT' . ($_SESSIONї'timezone'] >= 0 ? '+' : '') . var_export($_SESSIONї'timezone'],true);
?>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)
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
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)
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
.. 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.
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)
{
var stylesheet = new Array();
stylesheetї0] = "am.css";
stylesheetї1] = "pm.css";
var now = new Date();
var hour = now.getHours();
if (hour > 12)
{
document.getElementById(id).href = stylesheetї0];
}
else
{
document.getElementById(id).href = stylesheetї1];
}
}
</script>
</head>
<body onload='javascript:gimmick("foo")'>
hihi
</body>
</html>