Doing an API integration that requires timezones to be handed off in W3C format, while Drupal opts to store timezones in seconds.

Wondering what the clean way to do this is, rather than stripping the +/- sign, dividing by 3600 and adding it back on. There has to be a date transformation right?

FYI... W3C looks like this: -08:00 and Drupal looks like this: -28800

Trying to avoid this code...

// $val = '-28800';
$sec = substr($val, 1);
$hours = intval(intval($sec) / 3600);
$minutes = intval(($sec / 60) % 60);
$w3cTZ = substr($val, 0, 1) .
  str_pad($hours, 2, "0", STR_PAD_LEFT) .":".
  str_pad($minutes, 2, "0", STR_PAD_LEFT);
link|improve this question

44% accept rate
As this is more of a PHP question, you will probably get better responses on the main SO site. – MPD Nov 17 '11 at 1:08
It's true. But Drupal provides ways (and the Date module as well) to present/transform the data. May be a PHP thing though yeah. – doublejosh Nov 18 '11 at 21:16
feedback

1 Answer

Ended up this this function...

// Convert timezone from second in Drupal to WC3 standard
function timezoneConvert($val, $toSeconds = false) {
  if(!$toSeconds) {
    // Starts like: "-28800" and becomes: "-08:00"
    $sec = substr($val, 1);
    $hours = intval(intval($sec) / 3600);
    $minutes = intval(($sec / 60) % 60);
    $w3cTZ = substr($val, 0, 1) .
      str_pad($hours, 2, "0", STR_PAD_LEFT) .":".
      str_pad($minutes, 2, "0", STR_PAD_LEFT);
    return $w3cTZ;
  }
  else {
    $seconds = 0;
    $units = explode(':',$val,2);
    $seconds += $units[0] * 3600; // Hours
    $seconds += $units[1] * 60; // Minutes
    return $seconds;
  }
}
link|improve this answer
Note, in D7 $user->timezone is in this format "America/Los_Angeles" rather than seconds. Drupal 6 included this info but the column was called timezone_name – doublejosh May 11 at 17:52
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.