Showing the correct timezone?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psxmetal
Forum Newbie
Posts: 6
Joined: Sun May 31, 2009 11:25 pm

Showing the correct timezone?

Post by psxmetal »

Hey everyone currently my server runs on US EST time.

It just came to me that When posting it will always show EST time.

How can I go by showing the correct timezone results for the user that is surfing my site?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Showing the correct timezone?

Post by McInfo »

A. Use Javascript to convert the server's time to the client's time in the browser.

B. Use Javascript to find the offset between the client's timezone and UTC. Pass the offset back to the server using AJAX or in a link or form variable. Save the timezone offset in a session variable. Calculate the client's time on the server.

To display the client's timezone offset:

Code: Select all

<script type="text/javascript">
var date = new Date();
var offset = date.getTimezoneOffset();
document.write('UTC Offset: ' + parseFloat(offset) + ' Minutes');
</script>
To set your server to use UTC in php.ini:

Code: Select all

date.timezone = UTC
or at runtime:

Code: Select all

date_default_timezone_set('UTC');
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 10:10 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Showing the correct timezone?

Post by Weirdan »

Please refer to this thread: viewtopic.php?p=507769#p507769
McInfo wrote: Use Javascript to find the offset between the client's timezone and UTC
You can get current offset in this way, but you can't use it for dates in the past or future, because on that dates offset might be different. Again, please refer to the thread I linked above — it's not as simple as you seem to think.
Post Reply