Search
Other Sourcerer questions
Forum

Code showing current date rather than modified date?

Tad Ditman-Brunye's Avatar Tad Ditman-Brunye
Hello -

I am using the following Sourcerer (v10.0.0) code in my articles to display modified date:
{source}
<?php echo HTMLHelper::_('date', $displayData['item']->modified, 'c'); ?>" itemprop="dateModified">
<?php echo Text::sprintf('COM_CONTENT_LAST_UPDATED', HTMLHelper::_('date', $displayData['item']->modified, Text::_('DATE_FORMAT_LC3'))); ?>{/source}

This should show modified date. However, it is showing current date upon refreshing the page.

I checked in PHPMyAdmin database and the modified dates in #__content are correct for all items. But when you refresh a page, the above code is displaying current date instead of modified date.

This has been happening since I updated to PHP 8.1, but may have been occurring prior to that.

Thank you for any help!
Tad Ditman-Brunye's Avatar Tad Ditman-Brunye
Note that I also tried this code instead:
{source}
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('modified'));
$query->from($db->quoteName('#__content'));
$query->where($db->quoteName('id') . ' = ' . $db->quote($this->item->id));
$db->setQuery($query);
$modifiedDate = $db->loadResult();

if ($modifiedDate) {
    echo JHtml::_('date', $modifiedDate, 'DATE_FORMAT_LC3');
}
?>
{/source}

But it gives the error "Using $this when not in object context"

Note that I am trying to run this code in a module embedded in the article.
Peter van Westen's Avatar Peter van Westen ADMIN
In both codes you are referencing php variables (objects) that don't exist in the scope the code is executed in.
You can't just reference a php variable that is used in a completely different part/file of Joomla inside the code you push to Sourcerer. That is not how php works.

But… why not simply use Articles Anywhere to output the modified date?
regularlabs.com/articlesanywhere
Please post a rating at the Joomla! Extensions Directory
Tad Ditman-Brunye's Avatar Tad Ditman-Brunye
Thanks for the info. I use sourcerer to display the article date within a module, can I do the same with Articles Anywhere?
Peter van Westen's Avatar Peter van Westen ADMIN
Yes.
Please post a rating at the Joomla! Extensions Directory
Tad Ditman-Brunye's Avatar Tad Ditman-Brunye
Thanks, I got it and figured it out. Working well now!
Peter van Westen's Avatar Peter van Westen ADMIN
🙂👍
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription