March 23, 2012

Convert timezone in php

Question by Nishu

I have these timezones. I want to get the current datetime depends on the given timezone. The user will select either one timezone. so need to return current time.

ASKT
CDT
EDT
HST
MDT
MST
PDT

How can i convert? Please help

Answer by Starx

Use the DateTime Class

$time = time(); //Get the current time
$date = new DateTime($time, new DateTimeZone('Pacific/Nauru')); //Set a time zone
echo $date->format('Y-m-d H:i:sP') . "n"; //display date

$date->setTimezone(new DateTimeZone('Europe/London')); //set another timezone
echo $date->format('Y-m-d H:i:sP') . "n"; //display data again

This, way you don’t have to give the same timestamp as new argument every time like mishu’s answer.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!