0

The following PHP Code:

function serverTimeZone_offset($userTimeZone)
{
    $userDateTimeZone = new DateTimeZone($userTimeZone);
    $userDateTime     = new DateTime("now", $userDateTimeZone);

    $serverTimeZone     = date_default_timezone_get();
    $serverDateTimeZone = new DateTimeZone($serverTimeZone);
    $serverDateTime     = new DateTime("now", $serverDateTimeZone);

    return $serverDateTimeZone->getOffset($userDateTime);
}

function getDefineTimeZone($timezone)
{
    $userDateTimeZone = new DateTimeZone($timezone);
                 return new DateTime("now", $userDateTimeZone);
}

function getServerTimeZone()
{
    $serverTimeZone     = date_default_timezone_get();
    $serverDateTimeZone = new DateTimeZone($serverTimeZone);

    return new DateTime("now", $serverDateTimeZone);
}

$userDateTime   = getDefineTimeZone('America/Curacao');
$serverDateTime = getServerTimeZone();
$timeOffset     = serverTimeZone_offset('America/Curacao');

var_dump($userDateTime);
var_dump($serverDateTime);
var_dump($timeOffset); // the seconds is incorrect ?!?!

// adding the timezone difference
$userDateTime->add(new DateInterval('PT'.$timeOffset.'S'));

var_dump($userDateTime);

Will output:

object(DateTime)[2]
  public 'date' => string '2014-10-22 17:36:39' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'America/Curacao' (length=15)

object(DateTime)[3]
  public 'date' => string '2014-10-22 23:36:39' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Paris' (length=12)

int 7200

object(DateTime)[2]
  public 'date' => string '2014-10-22 19:36:39' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'America/Curacao' (length=15)

Which is obviously incorrect. The offset is returning back 7200 seconds (only 2 hours) rather than 21600 seconds (6 hours). Why?

1 Answer 1

1

I think uou are misinterpreting the behaviour of DateTimeZone::getOffset(). As said in DateTimeZone php docs:

This function returns the offset to GMT for the date/time specified in the datetime parameter. The GMT offset is calculated with the timezone information contained in the DateTimeZone object being used.

So if server timezone is Europe/Paris, then getOffset() will return the 7200 seconds, as Europe/Paris is GMT+01:00, and it is summer time right now, so it is GMT+02:00.

Try using this code instead:

function serverTimeZone_offset($userTimeZone)
{
    $userDateTimeZone = new DateTimeZone($userTimeZone);
    $userDateTime     = new DateTime("now", $userDateTimeZone);

    $serverTimeZone     = date_default_timezone_get();
    $serverDateTimeZone = new DateTimeZone($serverTimeZone);
    $serverDateTime     = new DateTime("now", $serverDateTimeZone);

    return $serverDateTimeZone->getOffset($userDateTime) - $userDateTimeZone->getOffset($userDateTime);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I indeed completely misinterpreted the behavior of getOffset. Thank you for the simplified clarification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.