Local time - Not local server time, user's local time
Moderator: General Moderators
Local time - Not local server time, user's local time
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Local time - Not local server time, user's local time
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.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.
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>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.
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.
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
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
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.But: no javascript, no formatting.
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>