How to convert datetime to another datetimezone in php???

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
hmdnawaz
Forum Newbie
Posts: 14
Joined: Tue Dec 21, 2010 10:55 pm

How to convert datetime to another datetimezone in php???

Post by hmdnawaz »

I have a varaiable which gets the server time;
$date =date("Y-m-d H:i:s");
suppose the server is in the USA then it the timezone of this date will be the timezone of USA.

And i want to convert this timezone to Asia/Karachi timezone.

Any Idea about this???
jankidudel
Forum Commoner
Posts: 91
Joined: Sat Oct 16, 2010 4:30 pm
Location: Lithuania, Vilnius

Re: How to convert datetime to another datetimezone in php??

Post by jankidudel »

Maybe manually count the difference in the seconds between 2 timezones:
$your_timezone = time();

$difference = hourdifference * minutedifference * 60;

$second_timezone = $your_timezone (+/-) $difference;

, then in the date function as optional argument(timestamp) insert $second_timezone, http://lt.php.net/manual/en/function.date.php
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How to convert datetime to another datetimezone in php??

Post by Weirdan »

http://us2.php.net/datetime :

Code: Select all

$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s'); // in your us tz
$dt->setTimeZone(new DateTimeZone('Asia/Karachi'));
echo $dt->format('Y-m-d H:i:s'); // Karachi tz
hmdnawaz
Forum Newbie
Posts: 14
Joined: Tue Dec 21, 2010 10:55 pm

Re: How to convert datetime to another datetimezone in php??

Post by hmdnawaz »

Thank you Weirdan. Thank you very much.
Post Reply