Forum Moderators: coopster

Message Too Old, No Replies

Checking your current time online

         

12inch

8:14 pm on Feb 23, 2006 (gmt 0)

10+ Year Member



I have created a test page wich shows the current GMT (Greenwich Mean Time) and the current local time of the visitor that views the test page.

Could you view this test page and check if the current time is correct?

Thank you.
I'm looking forward to the feedback.

Anand

[edited by: coopster at 8:21 pm (utc) on Feb. 23, 2006]
[edit reason] no urls please TOS [webmasterworld.com] [/edit]

coopster

8:23 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi Anand, please refer to our PHP Charter for Posting Guidelines [webmasterworld.com].

If you wanted to show some code we would be happy to help you confirm and troubleshoot any issues.

12inch

8:33 pm on Feb 23, 2006 (gmt 0)

10+ Year Member



Oops, sorry.

Below is the code that displays the GMT and the current local time of the visitor. I calculate the current local time with the Javascript function getTimezoneOffset and PHP.
I'm not sure if the code is correct, but it works for my local time. I don't know if it works in other countries.

I use a form, because I want to use the value from getTimezoneOffset (javascript).

Does anybody have any suggestions?

<?php

if (isset($_POST['tzoffset']))
{
$offset = -$_POST['tzoffset'];
$time = time() + (3600 * $offset);
$gmt = gmdate("Y-m-d H:i:s", $time);
echo "Your current time is: <strong>".$gmt. "</strong><br>";
}

echo "<br>GMT is: " .gmdate("Y-m-d H:i:s"). "<br><br>";
?>
<form method="post" id="testform" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="hidden" name="tzoffset">
<input type="submit" value="Show my current time">
</form>

<script type="text/javascript">
window.onload = function() {timezoneoffset();}

function timezoneoffset()
{
var now = new Date();
var offset = now.getTimezoneOffset()/60;
document.getElementById('testform').tzoffset.value = offset;
}
</script>

coopster

9:22 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It will indeed work, but what if the user's pc clock/time zone setting is invalid? Or what if they JavaScript disabled? Maybe that is of no concern though?

12inch

10:54 pm on Feb 23, 2006 (gmt 0)

10+ Year Member



The reason I'm calculating the local time of the visitor, is because the visitor can send a reminder to him/herself at a specified date and time. So it is important that the date and the time are correct.

Perhaps a pulldown menu with timezones is a good idea?
I can for instance use the timezones used in Windows (double click on the clock in the system tray and choose tab "Time Zone").

jatar_k

11:36 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> Perhaps a pulldown menu with timezones is a good idea

yes it is

I am assuming this a logged in user.

Just make it part of their profile to setup their timezone.