PDA

View Full Version : PHP time function depending on visitors time zone.



kgun
02-27-2010, 08:23 AM
Look at the heading of this Bleivik.no (http://www.bleivik.no/) site. That heading is produced like this:



function welcome() {
date_default_timezone_set('Europe/Oslo');
$hour = date('H');
if ($hour < 12)
{
$x = "morning";
}
elseif ($hour < 18)
{
$x = "afternoon";
}
else
{
$x = "evening";
}

$day = date('w');
switch($day)
{
case '0':
$y = "Sunday";
break;
case '1':
$y = "Monday";
break;
case '2':
$y = "Tuesday";
break;
case '3':
$y = "Wednesday";
break;
case '4':
$y = "Thursday";
break;
case '5':
$y = "Friday";
break;
case '6':
$y = "Saturday";
break;
default:
$y = "No day!";
break;
}
return array($y, $x, $_SERVER['HTTP_HOST']);
}
list($weekday, $interval, $host) = welcome();
?>
<div id="navigation">
<ul>
<li><?php echo "Welcome to " . "$host" . " this " . "$weekday" . " $interval"; ?> in Norway.</li>
</ul>
</div>

So the question is how is the best way to produce the heading depending on the visitor's time zone setting?

DaveSawers
02-27-2010, 11:26 PM
You need the time on the client which you can get from Javascript using:


d=new Date();
offset=d.getTimezoneOffset();
...

Which returns the users offset from GMT in minutes. Pass this to your PHP (AJAX) and use something like:


$zero = 0;
$timenow = date("D d M Y H:i:s");
$tserver = strtotime($timenow, $zero);
$timenow .= " UTC";
$tUTC = strtotime($timenow, $zero);
$diff = ($tserver - $tUTC)/60;
// echo("Client Difference:" . $offset . " Server Difference:" . $diff);
$offset = $diff - $offset;

This gives you the offset between the server and the client which you can then apply to your calculations.

NOTE - This example is not complete but I know kgun can fill in the missing bits. :D

kgun
02-28-2010, 05:12 AM
Thank you very much and congratulation with a great Olympic's arrangement. My already positive picture of Canada has been even better. May be the best multicultural country in the world.

Back to the problem. Since I posted the question, I have thought of this solution:



Determine the visitors real Ip (http://www.webproworld.com/internet-security-discussion-forum/99801-getting-visitors-real-ip-address.html#post498858).
Based on that determine the location (time zone) and cycle (switch) through an array with these elements: PHP: List of Supported Timezones - Manual (http://www.php.net/manual/en/timezones.php)
Your solution seem better.

Anybody with a finished solution?

DaveSawers
02-28-2010, 08:33 AM
Ooops. Ooops.

DaveSawers
02-28-2010, 08:33 AM
Didn't we do well at the olympics! However, for most Canadians, the only gold that matters is that for ice hockey. The gold medal match against the USA starts today at noon (Vancouver time)... Canada is probably the best country in the world although the Scandinavian countries run it close. I lived and worked all over the world before settling in Canada. I have no plans to leave. My brother thinks Norway is the best country in the world. He currently works in Stavanger.

My proposed solution is actually part of a finished solution whereby users need to know the date and time that a database record was modified. I just copied and pasted some code.

This is actually something that would be useful on the Webproworld forum where post times are GMT+1 rather than local times meaning it is very hard to figure out how long ago a post was made, unless you're in the UK. For example, your post above was allegedly made at 11:12am today. Since it's only 6:30am here as I type this it looks to me as if you have a time machine!

Your soution based on IP is flawed for a couple of reasons:

Some internet users have IP addresses that are in a different timezone. For example, satellite users in north and central America generally use the Hughes network whose ground station (and IP address locations) is in Washington DC.

When travelling, I keep my laptop on mountain time as then I know what time it is at home which is important for several reasons. The time zone I happen to be in is then often different to my computers (and my brains) time zone.

DaveSawers
02-28-2010, 08:53 AM
Another problem with IP address locations...

When I was in Mexico in December and January I used a satellite internet connection on the Hughes network so any time I searched for local businesses, Google's local search returned stuff in Washington DC which wasn't very helpful. Since you can set the timezone on your computer, why can't you set your latitude and longitude too. This is easy to find using Google maps or equivalent and is much more precise than using IP addresses.

Then when travelling, I would keep my time zone as mountain time and adjust my actual location to where I was staying. Then when I search for the Mexican restaurant closest to the hotel I'm in, I'll actually get useful results.

kgun
02-28-2010, 09:12 AM
Didn't we do well at the olympics! However, for most Canadians, the only gold that matters is that for ice hockey. The gold medal match against the USA starts today at noon (Vancouver time)... Canada is probably the best country in the world although the Scandinavian countries run it close. I lived and worked all over the world before settling in Canada. I have no plans to leave. My brother thinks Norway is the best country in the world. He currently works in Stavanger.

I know that ice hockey is your favourite sport, but what about the gold in ice dance and curling? When I was young, I thought of emigrating to Canada, since I love outdoor life. For me it is biathlon 4x7.5km relay and ski jumping (hopefully ski flying will become a future competition). Switzerland's Simon Amman is one of the best ski jumpers the world have ever seen.



This is actually something that would be useful on the Webproworld forum where post times are GMT+1 rather than local times meaning it is very hard to figure out how long ago a post was made, unless you're in the UK. For example, your post above was allegedly made at 11:12am today. Since it's only 6:30am here as I type this it looks to me as if you have a time machine!
I definitely agree to that. That has confused me many times.



Your soution based on IP is flawed for a couple of reasons:

Some internet users have IP addresses that are in a different timezone. For example, satellite users in north and central America generally use the Hughes network whose ground station (and IP address locations) is in Washington DC.

Great point.



When travelling, I keep my laptop on mountain time as then I know what time it is at home which is important for several reasons. The time zone I happen to be in is then often different to my computers (and my brains) time zone.
:roll:

I think the finale against USA will be hard. I never forget when the students from the USA beat Soviet Union.

And today it is also 50 km cross country skiing. My favourite is Lukas Bauer of the Czech Republic. He won the very hard Tour de Ski competition. Our own Petter Northug - Cross-Country Skiing Athletes : Vancouver 2010 Winter Olympics (http://www.vancouver2010.com/olympic-cross-country-skiing/athletes/petter-northug_ath1002473KM.html) had no chance to follow him. But the 50 Km in Vancouver is flatter. If he follows Lukas Bauer, Swiss Dario Cologna or who else to the finish he is strong in the finish.

DaveSawers
03-01-2010, 11:05 AM
Well. Petter Northug did win the 50km cross country and Canada won the hockey. Both competitions were very close but I guess now we're both happy. :D

kgun
03-01-2010, 11:45 AM
Definitely. I watched both, but did not watch the ending seremony with Neil Young, one of my favourite singers that I thought was American. It was too late for me, 02.30 Norwegian time. Hopefully I will see a reprise today.

What a hockey match. To me It was indifferent who won, but for the final seremony I preferred Canada. You would have been a little depressed if you lost;).

Wath about Sochi 2014 (http://sochi2014.com/) ? Then you will meet a much stronger Russian team and we will meet much stronger Russian cross country skiers.

A very good arrangement that you can be proud of.

No gold medal in former Canadian Olympic arrangements and now 14. That is amazing.