Sign in to follow this  

[web] PHP: Check users timezone

This topic is 3660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

A remote user - as in someone accessing the website? There isn't, that information isn't passed in HTTP GET requests.

Aside from asking the user to enter theit time zone, the best you could do would be to lookup their IP address to determine their country, and get the time zone from that. Although that won't always work, and requires a IP -> Country lookup, and as far as I know there's no free databases with that information.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
A remote user - as in someone accessing the website? There isn't, that information isn't passed in HTTP GET requests.

Aside from asking the user to enter theit time zone, the best you could do would be to lookup their IP address to determine their country, and get the time zone from that. Although that won't always work, and requires a IP -> Country lookup, and as far as I know there's no free databases with that information.


Yeah, I had a feeling that was the case. Option 1 is what I was looking at going for (having the users select their timezone), I was just looking for a cheeky alternative to save some time.

Thanks anyhow Steve.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
as far as I know there's no free databases with that information.

IP-to-country

Quote:
Original post by Sonnenblume
Yeah, I had a feeling that was the case. Option 1 is what I was looking at going for (having the users select their timezone), I was just looking for a cheeky alternative to save some time.

You can check the local time client-side using javascript.

Share this post


Link to post
Share on other sites
You can find out the local time their computer clock is set to with javascript. You can also find out what offset it thinks that has from GMT.

Sadly neither of these things will tell you the user's time zone (you might make an educated guess which is fairly likely to be wrong though)- for that you're just going to have to ask them.

Remember that a time zone is NOT just an offset from GMT.

Mark

Share this post


Link to post
Share on other sites
I'd solve it this way:
Quote:

<?php
if(isset($_GET['timezone'])){
if($_GET['timezone']=="f"){
echo ' Bad Luck.....javascript is not enabled';
}else{
$time_offset=-$_GET['timezone'];
echo 'Timezone is GMT '.$time_offset;
}
}else{
echo'

<script language ="javascript">
< !--
var dt = new Date();
var dtTimezoneOffset = dt.getTimezoneOffset() / 60;
document.location.href ="test.php?timezone="+dtTimezoneOffset;
// -- >
</script>
<a href="test.php?timezone=f">click here if javascript is not enabled</a>
';
}
?>


if javascript is not enabled, the redirect wouldn't workzo, the script receives timezone="f" (or whatever you like)

If you want you can add a meta redirect with 2 secs, if javascript is enabled, it will be faster, if not the script will know that js is not enabled.

You can save the value using SESSION or Cookies.

Share this post


Link to post
Share on other sites

This topic is 3660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this