0

Possible Duplicate:
Timezone conversion in php

Here is a simple example of what i want to do.

I have a variable $time containing a timestamp.

Now, I want to convert it into timestamp of different timezones

Is there a way to do this without using the function date_default_timezone_set() multiple times ?

I know my point might seem confusing and point less so please clarify any doubt in comments.

If I have, for example, 100 calls to change it:-

  1. It affects my other functions
  2. The functions name suggests its default
  3. It slows down the script.
Community
  • 1
  • 1
kritya
  • 3,312
  • 7
  • 43
  • 60

2 Answers2

2

You can use the DateTime object and set the time zone as required using the SetTimezone() method.

For example, the following code:-

$datetime = new DateTime();
var_dump($datetime->getTimezone()->getName());
var_dump($datetime->format(DateTime::ISO8601));
$datetime->setTimezone(new DateTimeZone('UTC'));
var_dump($datetime->format(DateTime::ISO8601));
$datetime->setTimezone(new DateTimeZone('America/New_York'));
var_dump($datetime->format(DateTime::ISO8601));

Gave me the following output:-

string 'Europe/London' (length=13)
string '2012-06-20T20:33:29+0100' (length=24)
string '2012-06-20T19:33:29+0000' (length=24)
string '2012-06-20T15:33:29-0400' (length=24)

My dev server default TZ is Europe/London, so you may get slightly different results.

There is a list of supported timezones here.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • Haha. I understand :P i also posted that from my android :P – kritya Jun 20 '12 at 18:28
  • Here are some DateTime-based timezone conversion examples - http://blog.serverdensity.com/2009/03/21/handling-timezone-conversion-with-php-datetime/ – anjunatl Jun 20 '12 at 18:30
0

To give you a sample of something ive done for creating a date in different time zones.. Here is what i have done

function create_date($format, $time, $offset = -6)
{   
    return gmdate($format, $time + (3600 * $offset));
}

If you want to keep this in timestamp format just modify it to look like this.

function create_date($time, $offset = -6)
{   
    return $time + (3600 * $offset);
}

UPDATE:

If you need to enter in timezone in text form I would use the list from PHP Timezone List question and then in the function do a simple lookup on that array to get the offset to make this work.

NOTICE:

for this to work correctly, the original time must be GMT time

Community
  • 1
  • 1
bretterer
  • 5,693
  • 5
  • 32
  • 53
  • This needs to enter the offset like the old method. Which means i must have offset for each timezone. When php has the data for us i would like to use it in some way :) – kritya Jun 20 '12 at 18:18
  • @kritya see update. I assume you mean you would want to do something like create_date(123456789,'US/Eastern'); – bretterer Jun 20 '12 at 18:22
  • What data do you have? Only one timestamp and you want to get that same timestamp 23 other times for all the other time zones? – NaturalBornCamper Jun 20 '12 at 18:25
  • Hmm. I have a single timestamp. And my script is in an infinite loop which monitors changes in a log file. Different users have different timezones and i would to convert it into that in which the user wants. A bit clear now ? – kritya Jun 20 '12 at 18:31