6

According to the documentation JFactory/getDate $tzOffset default value is 0. However, the following example:

$date_default_offset = JFactory::getDate('now');
echo $date_default_offset->toRFC822(true);
echo "<br />";
$date_zero_offset = JFactory::getDate('now', 0);
echo $date_zero_offset->toRFC822(true);

... gives different outputs:

Mon, 13 Oct 2014 18:31:37 +0000
Mon, 13 Oct 2014 19:31:41 +0100

Shouldn't both dates be the same? What else should I take into account?

The actual date is:

echo date("r"); // Mon, 13 Oct 2014 20:40:33 +0200

That's right because the server is in Spain (+1 GMT and +1 Daylight Saving Time in Summer).

The problem arised whe trying to adjust a module to take into account the current time in Canary Islands, that should be Mon, 13 Oct 2014 19:31:41 +0100according to the given example.

I'm using Joomla! 2.5.27, the Server Time Zone is set to Canary (GMT+0).

  • Did you try echo JHtml::date('now', 'Y-d-m H:i:s e', 'Atlantic/Canary');? – Farahmand Oct 13 '14 at 20:37
  • @Farahmand That gives exactly the date/time in Canary Islands. Need to study a bit more to understand the differences. However, note that the question is related to the 'default' behaviour in the getDate() construction. Shouldn't both getDate() calls give the same result? – pQB Oct 14 '14 at 7:25
  • Check my answer, @pQB. – Farahmand Oct 14 '14 at 12:00
4

Open the file /libraries/joomla/factory.php and search for getDate() function:

public static function getDate($time = 'now', $tzOffset = null)

The default value of time zone offset ($tzOffset) is null and not 0. It may be due to a mistake in Joomla documentation or it's for versions older than 2.5.

In Joomla 3, $date_zero_offset = JFactory::getDate('now', 0); raises an error:

DateTime::__construct() expects parameter 2 to be DateTimeZone, integer given

So for Canary Islands, you can use this:

$date_zero_offset = JFactory::getDate('now', 'Atlantic/Canary');
  • You are right. Thank for pointing the file. No doubts. However, the documentation I pointed in the question clearly states that $tzOffset is a signed integer which default value is 0, and was the root of the problem. – pQB Oct 14 '14 at 12:53
  • 1
    No problem. Joomla documentations like other documentations over the web may have faults. My suggestion for developers is using an IDE like Eclipse and check the core functions instantly. – Farahmand Oct 14 '14 at 13:10
3

It might be an idea to get the offset from your Global Config like so:

$date = JFactory::getDate($input='now', 'UTC');
$tz   = JFactory::getConfig()->getValue('config.offset');
$date->setOffset($tz);

echo $date->toFormat();

To make life a little easier and have less code, I would suggest using JHtml::date

echo JHtml::date($input = 'now', 'm/d/Y', false);

Hope this helps

  • Your example outputs 2014-10-13 19:05:52 however, the current time at Canary Islands in 20:05:52. Note +1 of difference because the +1 Daylight Saving Time in Summer. – pQB Oct 13 '14 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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