views:

69

answers:

3

Hello All and Greetings,

I'm working on an assignment which determines the client timezone offset , passes the same to a PHP script, which contains a code to fetch the timezone identifier for that client's area.

Now for determining timezone offset, I have used JavaScript. However, I need some way to pass this to PHP. I used AJAX call for the same.

var url = "index.php";
url=url+"?client_timezoneoffset="+offset;
xmlhttp.open("GET",url,true);
xmlhttp.send(true);

This JavaScript I'm calling onload of index.php. But still $_GET comes empty. I twisted this implementation and called another page set_client_timezone.php where to my surprise, $_GET contained the timezone. I then stored it in $_SESSION.

When I did an all-round testing of the application, to my surprise, the timezone changes were getting reflected on SECOND load of the page.

i.e. first time I navigated to http://www.example.com/index.php, it showed me time in UTC, but when I reloaded, it showed me correct time as per my timezone - Asia/Calcutta.

Can anyone of you guys out there suggest me a way out of this paradox ?

Can anyone point me a standard way of achieving the same (I'm ready to rollback my current implementation)

I'm posting in this forum for the first time and so heartfelt apologies if my post flouted any rule of this forum.

Cheers,
Abhijit.

A: 

If you change the timezone by ajax, then the php serverside stuff for loading the index.php on first call is already finished. Then after the index.php is finished the client side/javascript code starts Ajax request and sets the timezone into the session, so for the next reload the serverside php stuff knows the correct timezone.

To fix it you could try it the other way round. Set the timezone in php and pass it to your javascript code.

TheCandyMan666
Thanks for the quick reply. I'm using JavaScript to determine client timezone offset, depending upon which I need to set the default timezone for my PHP script...
Abhijit
How re you determining the timezone? Isn't it also possible by php? Besides that the only solution i could think of is, load everything (or all that is timezone dependant) by ajax. So you can be sure you already know what timezone is used to show the corresponding dataset.
TheCandyMan666
See... On page load of http://www.example.com/index.php I call the above JavaScript which returns me the offset in sec. I pass this to a set_client_timezone.php page using GET. In this page, I set it into $_SESSION.
Abhijit
Now in PHP there is a API function called timezone_abbreviations_list() that returns all timezone identifiers in an array with the (offset_from_GMT_in_sec, timezone_identifier, day_light_saving_time) entries. I use the above offset to retrieve the timezone from this array.
Abhijit
Is there any other standard way out for the same?
Abhijit
Thanks a lot for all the prompt responses.. :)
Abhijit
A: 

I agree with above suggestion. You can involk the js function after loading the main content(index.php) for solve this issue.

setTimeout("yourfunction()",1000);// load function after 1 seconds from the actual loading time

This may helps you.

Ajith
Thanks again... but it is not working... :(
Abhijit
A: 

Hello All ,

I just reworked on the loading basics of PHP and JavaScript and re-altered my code to make it work. Thanks all for the prompt feedback.

Anyone looking for a good and excellent solution to this issue, I direct you to a blog - http://www.incoherent.ch/blog/2008/02/20/php5-time-zone-solution/

Cheers,
Abhijit

Abhijit