Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

RFC 3339 Timezones in Joomla! 1.5

I wrote the following piece of code for my GSoC project this year but ended up not having to use it. Just found a backup copy of it on my hard drive now and thought I should post in case it might be of any use to anybody.

This generates the current timezone for use in RFC 3339 dates, obviously for use inside of Joomla! 1.5 but since it's commented I'm sure it can be easily adjusted for your needs.

This is a bit of a manual calculation but you can probably get what you need from the Joomla! 1.5 API.

Please note that this has never been tested properly so use at your own risk. :)

<?php

// Get timezone from configuration
$config =& JFactory::getConfig();
$offset = $config->getValue('config.offset');

// Split up the offset into hours and minutes according to the decimal point
list($hours, $minutes) = explode('.', $offset);

// Calculate the minutes according to the fraction of 60 minutes
$minutes = 60 * "0.$minutes";

// If the hours is non-negative, add the positive sign
if ($hours >= 0) {
  $hours = "+$hours";
}

// If the hours is represented as a single numeric value, pad with a zero
if (strlen($hours) == 2) {
  $hours = substr($hours, 0, 1) . '0' . substr($hours, 1, 1);
}

// If the minutes is represented as a single numeric value, pad with a zero
if (strlen($minutes) == 1) {
  $minutes = "0$minutes";
}

// Generate the timezone string
$timezone = "$hours:$minutes";

?>

0 Comments

Post a Comment

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.