Forum Moderators: coopster

Message Too Old, No Replies

php time for another timezone

         

greencode

8:12 pm on Jan 26, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



I know how to display the current users time in php as this:

<?php
echo date("G:i");
?>

to display as: 20:09

but is there any way that I can display the time for another timezone?

I'm wanting to display the time for a few different countries like this:

London: 20:09
New York: 15:09
Sydney: 07:11

Is there any way of doing this simply?

Matthew1980

9:31 pm on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there greencode,

Yes there are ways of doing it something like this:-

I'm approximating the time because I dont know the exact differences but hopefully you can see the mechanics of it.

$new_york_time = "New york: ".date("G:i", strtotime(" + 5 hours"));
$Tokyo_time = "Tokyo: ".date("G:i", strtotime(" + 8 hours"));
$sydney_time = "Sydney: ".date("G:i", strtotime(" + 11 hours"));

just echo the vars from there

From what I understand the strtotime can be used as an offset, I havnt tried this code but as long as its concatonated you should be ok. I use a version of this as my server time stamp and have to alter it by a few hours, if you want this to do the same thing from any terminal on the planet I dont think it would work, this is relative to the server that the php is being executed on.

Hope that makes sense, either way have a play and you should find some combination that will work.

Cheers,

MRb

greencode

10:33 pm on Jan 26, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for this - I'll give this a go tomorrow and report back.

greencode

10:47 pm on Jan 26, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Actually I've just given it a go and it appears to work. But as you said the problem is is someone looks at the website in another country then this will show the incorrect time.

I have this:
<?php
$new_york_time = "New york: ".date("G:i", strtotime(" - 5 hours"));
echo "$new_york_time";
?>

which is showing correctly in the UK but I would think if you looked at this in a country that is not currently showing GMT then it wouldn't be correct?

Matthew1980

11:47 pm on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Greencode,

My first thought on this is that to get the users time/date will include some sort of javascript as that is operated client side, and then fuse it with php to get what you are after. I know it sounds long winded but thats one way that springs to mind at the moment.

MRb

dreamcatcher

8:00 am on Jan 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The PHP date function returns the server time. This will be the same for all users no matter what country they are in. If you adjust the time by 5hrs, that will be the same for everyone.

If you are running PHP5, look into date_default_timezone_set [php.net] which enables you to set the default timezone for your script. You can find a list of supported timezones here [php.net].

If you want users to see the time in their own zone, you`ll have to provide the user with a time adjustment feature and set a cookie on the users machine. Like many message boards have. The PHP website provides all the info you should need with regards to Date & Time Functions [php.net].

dc