4

I've been struggling to get the correct date and time in a module.

I set global config to London (i.e. GMT which is currently 1 hour ahead of UTC)

I set the date in my component to

2020-05-10 12:50:00 (calendar select box)

and this seems to work. It's stored as

2020-05-10 11:50:00 in the database - so the timezone is being correctly accounted for.

I then go to retrieve it in my module adjusting for the time zone

        $config = JFactory::getConfig();
        $debug = $config->get('offset');
        $myDateString = $itemStartDate; //the date stored in the db 2020-05-10 11:50:00

        $date = new JDate($myDateString);
        $date->setTimezone($debug);

        echo "database date is ".$itemStartDate." and calculated date is ".$date->format(JText::_('DATE_FORMAT_FILTER_DATETIME'));

The date shown by the echo statement is just the date from the database - it doesn't account for the 1 hour time difference.

Am I doing something wrong?

I looked in config and there is offset and offset_user - offset is set to Europe/London and offset_user to UTC (although when I manually change it to Europe/London is makes no difference)

thanks

0

1 Answer 1

4

Answer in the update. Just needed the correct joomla code!

Okay seemed to have worked it out.

        $todayDate = JFactory::getDate('now', $timezoneis);
        $date = JFactory::getDate($itemStartDate, $timezoneis);
        $date2 = JHtml::date($itemStartDate , 'Y-m-d H:i:s', $timezoneis);
        echo "<br>database date is ".$itemStartDate;

        echo " <br>Timezoned date is ".$date." timezone: ".$timezoneis;
        echo " <br>Corrected timezoned date2 is ".$date2." timezone: ".$timezoneis;
        echo "<br>todays date is ".$todayDate;

$date2 variable gives the corrected time regardless of time zone. I also corrected $todays date so that it gives todays date correctly.

This is really badly documented in Joomla so I hope this helps someone struggling to figure it out.

2
  • Please accept your answer so that the system stops bumping this question for new attention. Feb 6, 2021 at 11:03
  • Does this take into account the change back during the year too?
    – Eoin
    Apr 29, 2021 at 9:41

Your Answer

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

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