...

View Full Version : how do i auto adjust time base on user local time



mrx
10-19-2006, 07:35 AM
how do i auto adjust time base on user local time

i want to put in a time like 1:00pm us time manually, how can i make this number automaticly change to whaterver time equivalent in user local timezone



<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
var time = "10:30 am";
</script>
</head>

<body>
<script>document.write(time)</script>
</body>
</html>

Philip M
10-19-2006, 09:18 AM
You can't as you have no way of knowing which time zone the user is in.

I am in the UK and this forum's clock tells me the time is 11.15 pm when it is actually 08.15 am.

CFMaBiSmAd
10-19-2006, 10:50 AM
The following is one possible way of doing this (I did not verify that the logic is correct for all possible time differences) All hour values are 24 hr format: 0-23 -

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
var serverhour = <?php echo date('H') ?> // server hour 0-23

var d=new Date()
var visitorhour=d.getHours() // visitor hour 0-23

var adjust = serverhour - visitorhour // calculate the difference

<?php
$event_h = 10;
$event_m = 30;
?>
var etime = new Date()
etime.setHours(<?php echo $event_h ?>,<?php echo $event_m ?>) // set hour:min to 10:30

var localetime = new Date()
localetime.setHours(etime.getHours()+adjust,etime.getMinutes()) // add the difference to the hours

</script>
</head>

<body>
<script>document.write('The event at: '+etime.getHours()+':'+etime.getMinutes()+' server time, will occur at: '+ localetime.getHours()+':'+localetime.getMinutes()+' visitor\'s local time.')</script>
</body>
</html>This example uses PHP to generate the javascript. The serverhour variable is set to the current hour on the server. The visitorhour variable is set to the current hour from the visitor's browser and the difference between the two hour values is calculated. The etime variable is set to the event time at the server. The localtime variable is set to the etime variable with the hours adjusted by the difference between the server and local time.

Kor
10-19-2006, 11:36 AM
You can't as you have no way of knowing which time zone the user is in.

I am in the UK and this forum's clock tells me the time is 11.15 pm when it is actually 08.15 am.

Oh, but you can:


<script type="text/javascript">
var today= new Date();
alert(-today.getTimezoneOffset());//TimeZone in minutes
</script>

CFMaBiSmAd
10-19-2006, 05:14 PM
For Philip M:

To get the times displayed in the Forum to be corrected for your time zone, go to the User CP, pick Edit Options, and change your time zone under the data and time options. The times shown on the posts and the last visited time will be corrected to when they occurred in your local time zone.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum