Page 1 of 1
Local time - Not local server time, user's local time
Posted: Sat Nov 11, 2006 4:50 am
by survient
I'm looking to create a dynamic php script that will determine a user's time zone, and display a clock with their time zone accordingly. I've seen ads use similar to find your location, like if I'm in New York, it will say "Find young singles in New York" or whatever. I'm hoping to use javascript or another local script as a last resort. I've tried many athings, and have failed at every turn. The closest was when I found a site that explained IP addresses, and how to determine location based upon them, however I was finding it to be unreliable, probably due to a code error on my part. So I ask, is there a way? thx.
Posted: Sat Nov 11, 2006 5:06 am
by volka
You might use something like
GeoIP to guess the country from the ip address.
Posted: Sat Nov 11, 2006 1:11 pm
by survient
ok, that helps out a lot, one more, is there any php databases with a function to convert from a city+country to timezone?
Posted: Sat Nov 11, 2006 5:52 pm
by Ambush Commander
Re: Local time - Not local server time, user's local time
Posted: Sat Nov 11, 2006 6:06 pm
by m3mn0n
survient wrote:I'm looking to create a dynamic php script that will determine a user's time zone, and display a clock with their time zone accordingly.
If you want a client variable like a user's timezone/time, you'll need them to define it and extract it from their info row in the db.
Or you could do something like this JavaScript snippet which doesn't require user input:
Code: Select all
<b>Todays Date: </b><SCRIPT LANGUAGE="JavaScript">
<!--
Stamp = new Date();
document.write('' + (Stamp.getMonth() + 1) +"/"+Stamp.getDate()+ "/"+Stamp.getYear() + ' <br> ');
var Hours;
var Mins;
var Time;
Hours = Stamp.getHours();
if (Hours >= 12) {
Time = " P.M.";
}
else {
Time = " A.M.";
}
if (Hours > 12) {
Hours -= 12;
}
if (Hours == 0) {
Hours = 12;
}
Mins = Stamp.getMinutes();
if (Mins < 10) {
Mins = "0" + Mins;
}
document.write('<B>Todays Time:</b> ' + Hours + ":" + Mins + Time + '');
//-->
</SCRIPT>
Posted: Sat Nov 11, 2006 6:59 pm
by Cameri
Well, another way you could do it is using AJAX.
You pass the user's local time through an HTTP Get Request from Javascript to PHP.
Posted: Sun Nov 12, 2006 12:07 am
by survient
I appreciate the help, however setting the default timezone can't help me get the local user's time zone, the whole idea of this is automation, so that the user does not have to input their time zone(the common man in the US usually doesn't know his GMT +-), and I want this to work with javascript disabled browsers, otherwise I wouldn't use php, I'd use javascript. There are properties of a users internet IP address that all these Geographical IP companies use to figure out the country, the city, etc, usually through a database, however the approximate longitude and latitude(of their ISP, I believe) can be found with a simple calculation, I can't remember what it is, and then using the longitude and latitude in another equation, you can find the time zone, I just can't remember the site I saw it on, or how to do it myself. Any pure php(or any other serverside language) solutions I would greatly appreciate. Again, thx for setting the default time zone func. I didn't know about it, it will be helpful once I store the user's time zone, and the javascript ones I have for my records, as well as I didn't know about AJAX, I've been looking for that. I'm going to continue my quest for a pure php solution, I need it especially for mobile devices, such a cell phones and PSPs, whose browsers are extremely crappy and can't handle javascript. thx.
Posted: Sun Nov 12, 2006 12:13 am
by feyd
Why not let the user choose? Make a best guess attempt on the server, but allow them to select it easily if it's not correct.
Posted: Sun Nov 12, 2006 12:24 am
by survient
That would be suicide, if I didn't let them choose. Of course I would, I'm just trying to make it as easy and streamlined as possible. We might be in the information age, but not everybody got the information.
Posted: Sun Nov 12, 2006 1:36 am
by me!
Every time I have seen a user time correction it’s in GMT, and the user needs to set there offset for there account.
Posted: Sun Nov 12, 2006 2:32 am
by survient
It's simply just because everybody uses phpbb, or stylizes their forum after it, and they decided to make it so the user had to set the time correction, it's a lot of work to automate it like that, and not accurate enough to them to make it worthwhile, to get 99% accuracy for a user's timezone you have to pay out the rear to a Geolocation service to access their premium databases. They probably didn't want to do all that work. I do, because I believe in making it as easy for the user as possible, so they don't have to worry about it. A sample of what I mean is, when they create their account, they could set the GMT, and it would preset it to what the calculation finds, also, next to the GMT +-, there would be the current time of that GMT zone, both 24 hour and 12 hour format. Also, for annonymous users, it would set the GMT setting for them to whatever the calculation predicts. A lot of work for the programmer, yes, but ultimately easing the process of signing up for a forum. That is my principle, and I'm sticking to it.
Posted: Sun Nov 12, 2006 5:17 am
by m3mn0n
Sure, maybe most people don't know their GMT +/- but I'm sure most, if not all know their timezone name. Thus, making a list with entries like ( Mountain - GMT + 0700 ) and etc. should be suitable.
Your other option is to buy/subscribe to a list of geocoding data and then you can go from there.
I knew of a really good site but I didn't bookmark it and a few Google search didn't bring it up.
A good keyword for this sort of thing is "geocoding", go wild
Posted: Sun Nov 12, 2006 5:52 am
by volka
Afaik you can't get the timezone from javascript, only the offset from utc. For me that changes twice a year. GMT+1->GMT+2 and back, i.e. CET<->CEST. Just like PST<->PDT, only on different dates (as if date/time isn't difficult enough). The timezone info for Europe/Berlin holds that kind of information. But as mentioned neither CET nor Europe/Berlin can be provided by javascript. A java applet could do the trick.
You might also let the client do the work. Just send it the utc timestamp and let it format the value.
Code: Select all
<?php $ts = time(); ?>
<html>
<head>
<title>datetime test</title>
<script type="text/javascript">
function clientsideDateformat(gmts, id) {
o = document.getElementById(id);
if ( null!=o ) {
lt = document.createTextNode(new Date(gmts).toLocaleString());
o.replaceChild(lt, o.firstChild);
}
}
</script>
</head>
<body>
<span id="currentTime"><?php echo gmdate('F j, Y, g:i a', $ts); ?></span>
<script type="text/javascript">clientsideDateformat(<?php echo $ts; ?>, 'currentTime');</script>
</body>
</html>
But: no javascript, no formatting.
Posted: Sun Nov 12, 2006 11:54 pm
by survient
yeah that's similar to what I used to use, before I found out the javascript was failing on some browsers. If I get this to work, and find the equation, I'll post it here.