0

I found some solutions recently and decided to go with this one so I created following method within one of my classes:

public static function date($format, $time = false, $from_timezone = false, $to_timezone = false) {

    if (!$time) {
        $time = time();
    }

    if (($from_timezone === $to_timezone) || (!$from_timezone || !$to_timezone)) {
        return date($format, $time);
    }

    $from_tz = new DateTimeZone($from_timezone);
    $to_tz = new DateTimeZone($to_timezone);

    $dt = new DateTime();
    $dt->setTimezone($from_tz);
    $dt->setTimestamp($time);

    $offset = $to_tz->getOffset($dt);

    return date($format, $dt->format('U') + $offset);
}

Then I did a simple test - I converted datetime to some other timezone, and then convert the result back to the original timezone with expectations to get the original datetime.

    $format = 'Y-m-d H:i:s';
    $initialtime = '2013-06-13 12:00:00';

    echo $initialtime;
    echo '<br/>';

    $convtime = TimezoneLib::date($format, strtotime($initialtime), 'Canada/Atlantic', 'Europe/Prague');
    echo $convtime;
    echo '<br/>';

    echo TimezoneLib::date($format, strtotime($convtime), 'Europe/Prague', 'Canada/Atlantic');
    die();

This is the output

2013-06-13 12:00:00 // Original
2013-06-13 14:00:00 // Converted
2013-06-13 11:00:00 // Converted to original timezone

Why dont they match? What am I missing? Thank you.

UPDATE

I am still unable to get matching dates even after removing strtotime. I found out that default timezone also affected DateTime objects. I came up with this:

<?php

class TimezoneLib {

    public static $defaultSystemTimezone;

    public static function init() {
        self::$defaultSystemTimezone = date_default_timezone_get();
    }

    public static function date($format, $time = false, $from_timezone = false, $to_timezone = false) {

        self::switchSystemTimezone($from_timezone);

        if (!$time) {
            $time = time();
        }

        if (($from_timezone === $to_timezone) || (!$from_timezone || !$to_timezone)) {
            return date($format, $time);
        }

        $from_tz = new DateTimeZone($from_timezone);
        $to_tz = new DateTimeZone($to_timezone);

        $dt = new DateTime($time, $from_tz);

        self::switchSystemTimezone($to_timezone);
        $offset = $to_tz->getOffset($dt);
        $convertedDate = date($format, $dt->format('U') + $offset);

        self::restoreSystemTimezone();
        return $convertedDate;
    }

    public static function switchSystemTimezone($timezone_identifier) {
        return date_default_timezone_set($timezone_identifier);
    }

    public static function restoreSystemTimezone() {
        return date_default_timezone_set(self::$defaultSystemTimezone);
    }

}

TimezoneLib::init();

$format = 'Y-m-d H:i:s';
$initialtime = '2013-06-13 12:00:00';

echo $initialtime;
echo '<br/>';

$convtime = TimezoneLib::date($format, $initialtime, 'Canada/Atlantic', 'Europe/Prague');
echo $convtime;
echo '<br/>';

echo TimezoneLib::date($format, $convtime, 'Europe/Prague', 'Canada/Atlantic');
die();

With following output

2013-06-13 12:00:00
2013-06-13 19:00:00
2013-06-13 11:00:00
  • 3
    strtotime will use your default timezone setting from php.ini and that is likely not the same as '2013-06-13 12:00:00'; Verify with http://php.net/date_default_timezone_get. – Gordon Jun 12 '13 at 13:09
  • you are right. I didnt really think of that. thank you for such a quick response! –  Jun 12 '13 at 13:14
  • no problem. feel free to self answer or delete. – Gordon Jun 12 '13 at 13:14
  • I thought I would be able to sort it out with your help with [date_default_timezone_set](http://php.net/manual/en/function.date-default-timezone-set.php) but it didn't work well so I updated my question. Is that wrong approach? –  Jun 12 '13 at 14:06
  • 1
    Well, it's overly complicated. I mean, what's the purpose of that TimezoneLib anyway? [Changing a Timezone](http://stackoverflow.com/questions/2505681/timezone-conversion-in-php/2505687#2505687) is fairly straightforward, so why would you need that class? – Gordon Jun 12 '13 at 14:20

1 Answers1

1

Create a new DateTime object instead than using strtotime:

$date = new DateTime('your_date_here',  DateTimeZone object here);
silkfire
  • 24,585
  • 15
  • 82
  • 105