Solved

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

Posted on 2008-06-25
6
1,946 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress type image upload 10 30
regex expression 9 23
using hash in login 34 13
Adding through query php 9 11
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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now