Solved

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

Posted on 2008-06-25
6
1,975 Views
Last Modified: 2013-12-12
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?
0
Comment
Question by:gingera
6 Comments
 
LVL 6

Accepted Solution

by:
mmarth earned 200 total points
ID: 21866801
$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
 

Author Comment

by:gingera
ID: 21866848
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
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 100 total points
ID: 21867130
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 2

Assisted Solution

by:mriz81
mriz81 earned 150 total points
ID: 21868746
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
 

Assisted Solution

by:cannot-be-changed
cannot-be-changed earned 50 total points
ID: 21882395
"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
 

Author Closing Comment

by:gingera
ID: 31470620
Thanks for your help.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question