Date and time

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
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Date and time

Post by amir »

Hello,

We have different date and time functions to get server date and time but is there any way in PHP that we could display date and time of a specific place?

For example, if I want to get current date and time of Toronto, Canada. How can I do that?

Any help would be greatly appreciated.

TIA!
Joke: Some might think that first I should know about a server which is in Toronto, Canada and then I should have my site on that server.
:D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Create a timestamp that is a central location (like GMT) then adjust that time to the timezone you wish to display.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

As far as I know, there's no function that accepts 'Toronto, Canada' as arguments. I think the best you can do is use gmdate() to get the GMT time, then use strtotime() to apply the offset to get the time in Toronto (-5 I think).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

Please advise!

I can calculate toronto canada time by my server time minus toronto canada's time. Is it a proper technique?

For example, my server time is
Current Server Time:9:10:53 PM (Mon)
and I know the current toronto canada time is
Monday, January 15, 2007 at 11:11:53 AM EST

and if minus 600 minutes from my server time then I get the date and time of toronto canada time.

Is it OK? if not how can I make it OK.
Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If you have php 5.2

Code: Select all

$tz = timezone_open('America/Toronto');
$dt = date_create('now', $tz);
echo $dt->format(DATE_RFC822);
will do.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

what about this one?

Code: Select all

echo 'Current time in Toronto: ' . date('m/d/Y H:i:s', time() - date('Z') + -5 * 3600);
Thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'd rather use the zoneinfo, like in java, c# and many more. But it might take a while until php 5.2+ is standard ;)
classic037
Forum Newbie
Posts: 4
Joined: Sun Jan 14, 2007 11:23 pm
Location: Mumbai
Contact:

Post by classic037 »

hey,

try this..

Code: Select all

<?
echo "Original Time: ". date("h:i:s")."\n";
putenv("TZ=US/Eastern");
echo "New Time: ". date("h:i:s")."\n";
?>
find valu for Toronto, Canada in putenv function... like


find time zone(TZ) list in given url:
http://www.theprojects.org/dev/zone.txt
Post Reply