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');
| improve this answer | |
  • 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
  • 2
    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

| improve this answer | |
  • 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
0

Couldn't get the date correctly in Joomla! v.3.9.14, here's my (proven) solution:

$oUser_TZ  = JFactory::getUser()->getTimezone();
$aUser_tz  = (array)$oUser_TZ; // almost sure this step is not that necessary
$full_date = JFactory::getDate('now', $aUser_tz['timezone']); // pretty sure $oUser_tz->timezone will work

// I had try to use $full_date->Format('Y-m-d H:i:s') but it was giving me the non-converted-to-wanted-timezone date, so
$date_converted = substr($full_date, 0, 19);

$date_converted gives me the date in format Y-m-d H:i:s and in the wanted timezone.

| improve this answer | |
  • 1
    Replace the entire snippet above with JHtml::date('now', 'Y-m-d H:i:s') The 2nd parameter is the desired format. The 3rd (omitted) parameter defaults to true -- this tells the function to use the user's designated timezone. docs.joomla.org/API17:JHtml::date I just tested my advice locally to be equivalent. – mickmackusa Jan 15 at 23:45
  • After a bit more thought, I don't think this question is focusing on generating a logged in user's datetime. It is about the 2nd param of getData(). Your answer would be more appropriate at joomla.stackexchange.com/q/10658/12352 ...but my advised one-liner is already there. – mickmackusa Jan 16 at 12:02
  • Sorry for the delay and thank you for your correction and advice @mickmackusa!, I tried it and it work like a charm!, very very helpful! I'm still struggling trying to understand the documentation of Jommla! classes and how to uses them properly. – ecedenyo Mar 16 at 21:53

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.