PHP: How to convert local time to another time zone?

Hello,

How do I convert local time to another time zone?

time() gives the current local time. How do I manipulate it to time in another time zone?
gingeraAsked:
Who is Participating?
 
mmarthConnect With a Mentor Commented:
$timenow = strftime("%H:%M:%S",mktime(date("H") + ($timeOffset), date("i"), date("s"), date("m") , date("d"), date("Y")));

$timeOffset is the difference between local time and needed time
0
 
gingeraAuthor Commented:
Thanks mmarth. Because of daylight saving in different parts of the world, the time offset may change during the year. So your suggested solution needs manual changing when daylight saving starts and ends.

Is there a better solution that uses
date_default_timezone_set("America/New_York"); to convert local time to New York time?
0
 
hernst42Connect With a Mentor Commented:
You could try to use the PHP objects DateTime and DateTimeZone, but currently those classes are not very well documented on php.net

Here is the docu extracted from ZendStudios default:

class DateTime {
    /**
     *
     *
     * @return DateTime
     * @param  time string[optional]
     * @param  object DateTimeZone
     */
    function __construct($time = null, $object) {}

    /**
     *
     *
     * @return string
     * @param  object DateTime
     * @param  format string
     */
    function format($object, $format) {}

    /**
     *
     *
     * @return void
     * @param  object DateTime
     * @param  modify string
     */
    function modify($object, $modify) {}

    /**
     *
     *
     * @return DateTimeZone
     * @param  object DateTime
     */
    function getTimezone($object) {}

    /**
     *
     *
     * @return void
     * @param  object DateTime
     * @param  object DateTimeZone
     */
    function setTimezone($object, $object) {}

    /**
     *
     *
     * @return int
     * @param  object DateTime
     */
    function getOffset($object) {}

    /**
     *
     *
     * @return void
     * @param  object DateTime
     * @param  hour long
     * @param  minute long
     * @param  second long
     */
    function setTime($object, $hour, $minute, $second) {}

    /**
     *
     *
     * @return void
     * @param  object DateTime
     * @param  year long
     * @param  month long
     * @param  day long
     */
    function setDate($object, $year, $month, $day) {}

    /**
     *
     *
     * @return void
     * @param  object DateTime
     * @param  year long
     * @param  week long
     * @param  day long
     */
    function setISODate($object, $year, $week, $day) {}

};

class DateTimeZone {
    /**
     *
     *
     * @return DateTimeZone
     * @param  timezone string
     */
    function __construct($timezone) {}

    /**
     *
     *
     * @return string
     * @param  object DateTimeZone
     */
    function getName($object) {}

    /**
     *
     *
     * @return int
     * @param  object DateTimeZone
     * @param  object DateTime
     */
    function getOffset($object, $object) {}

    /**
     *
     *
     * @return array
     * @param  object DateTimeZone
     */
    function getTransitions($object) {}

    /**
     *
     *
     * @return array
     */
    function listAbbreviations() {}

    /**
     *
     *
     * @return array
     */
    function listIdentifiers() {}

};
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
mriz81Connect With a Mentor Commented:
You will first set the timezone by putenv function and then print the date to get date/time of the desired timezone.

Complete list of Timezones can be found at http://www.expertsrt.com/tutorials/Matt/zone.tab.txt

So to get Japan Time you will write


putenv ('TZ=Asia/Tokyo');
echo date("Y-m-d H:i:s");

Open in new window

0
 
cannot-be-changedConnect With a Mentor Commented:
"Because of daylight saving in different parts of the world, the time offset may change during the year."

The best way to approach and develop a decent system depends on how your site works.
I don't know exactly WHAT purpose you have for changing times, so advising a best approach is difficult.

On a site I did recently, all times where for members only, and so on registration I made them enter their current time and from that form I stored their gmt time (gmt+1 gmt +5 gmt-4 etc)

Then all saved/stored times throughout the site I simply take a gmt time stamp and store it in the DB in the relevant table.
Then for displaying times, I simply take the gmt timestamp, multiply the users gmt time number by 3600, 3600 being number of seconds on an hour, and that gives you a new timestamp.
The new timestamp is the stored timestamp with seconds added or subtracted depending on the users gmt time.
So if the usre is gmt+4 it will add 4 hours to the original stored  time and thus will display whatever the stored time was (maybe another members last login or whatever) and will show it in the viewing users time.

So you can stil use GMT if you just want to display different timezones. GMt is the best thing to work from in my opinion, as it makes daylight changes easier.
So for example, Japan would be GMT+11 (or whatever)
0
 
gingeraAuthor Commented:
Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.