0

How to show the time and data in the user's timezone? I don't know it at all! Can anyone teach me? Thanks!

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by digital-ether
0

I use this code found from one forum.
Hope this helps you.

<?php
if (!isset($_POST["timezoneoffset"])){
?>
 <form method="post" action="<?php echo $_SERVER["PHP_SELF"];  ?>" id="time_form" name="time_form">
 <script type="text/javascript">
  tzo = - new Date().getTimezoneOffset()*60;
  document.write('<input type="hidden" value="'+tzo+'" name="timezoneoffset">');
 </script>
 <input type="submit" value="Get Server Client TimeZone Difference" name="Ok">
 </form>
<?php
}else{
 $serverTimezoneOffset = (date("O") / 100 * 60 * 60);
 echo 'Server Timezone Offset : ' . ($serverTimezoneOffset/(60*60)) .' hours';
 echo '<br />';
 $clientTimezoneOffset = $_POST["timezoneoffset"];
 echo 'Client Timezone Offset : ' . ($clientTimezoneOffset/(60*60)) .' hours';
 echo '<br />';
 $serverTime = time();
 echo 'Server date time : ' . strftime("%d %b %Y %H:%M", $serverTime);
 echo '<br />';
 $serverClientTimeDifference = $clientTimezoneOffset-$serverTimezoneOffset;
 $clientTime = $serverTime+$serverClientTimeDifference;
 echo 'Client date time : ' . strftime("%d %b %Y %H:%M", $clientTime);
 echo '<br />';
 echo 'Difference : ' . ($serverClientTimeDifference/(60*60)) . " hours";
 echo '<br />';
}
?>
0

You probably need to do this via js, BUT instead of a form to post, why not do an ajax call if a flag isn't set (flag can be a session var). Once the tz is set, flag also set so the ajax call isn't run more than once during a session. I'd avoid using a normal cookie for this, but I suppose you could. Gets a bit messier if the user travels a lot though.

e.g.

<head>
...
<?php
if(!isset($_SESSION['tz_call'])){
  echo "<script src=\"scripts/tz.js\"></script>";
}
?>
...
</head>

Edited by diafol: n/a

0

I don't want to have the submit button. Is it possible? If it can, how to rewrite the code? Thanks!

Edited by cliffcc: n/a

0

I don't want to have the submit button. Is it possible? If it can, how to rewrite the code? Thanks!

You can use a redirect, with cookies or session.

eg:

<?php 
@session_start();
if (isset($_GET['tz'])) {
  $_SESSION['tz'] = $_GET['tz'];
} else if (!isset($_SESSION['tz'])) { ?>
<script>
window.location = '<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?tz=' + (new Date().getTimezoneOffset()/60);
</script>
<?php } ?>

Note that using JS assumes the clients clock is set to the correct timezone. Another option is to use the users IP and a geocoding service to guess their timezone.

Edited by digital-ether: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.