Keyword

[solved] how to get the creation date of the previous item

  • Wagner Anghinoni Nardin
  • Wagner Anghinoni Nardin's Avatar Topic Author
  • Offline
  • New Member
  • Web designer in Brazil, Joomla! and K2 intusiast.
More
8 years 2 months ago - 8 years 2 months ago #156381 by Wagner Anghinoni Nardin
[solved] how to get the creation date of the previous item was created by Wagner Anghinoni Nardin
Hi

I need to get the creation date of the previous item for comparison and later use?

I have a case where I display the items in order of date, but the date of creation should appear only on the first item of the day.

I believe that taking the date of the previous item, I can create an IF which shows the date of creation only if the current date is different from the previous item.

Are there other ways?


As an example, the attached image contains news in order of date, but the date of creation should appear only on the first item of the day.

Last edit: 8 years 2 months ago by Wagner Anghinoni Nardin. Reason: solved

Please Log in or Create an account to join the conversation.

  • Wagner Anghinoni Nardin
  • Wagner Anghinoni Nardin's Avatar Topic Author
  • Offline
  • New Member
  • Web designer in Brazil, Joomla! and K2 intusiast.
More
8 years 2 months ago #156387 by Wagner Anghinoni Nardin
Replied by Wagner Anghinoni Nardin on topic [solved] how to get the creation date of the previous item
I solved the problem with the code below, which increase the current item ID to query the DB to the new item (if one exists), then return to your date and compare it with the date of the current item.

<div class="divCatItemDateCreated">
<?php if($this->item->params->get('catItemDateCreated')): ?>
<!-- Date created -->
<?php
$idAnterior = (( $this->item->id )+ '1');
$db1 = JFactory::getDBO();
$query1 = $db1->getQuery(true);
$query1->select('created');
$query1->from($db1->quoteName('xnrhd_k2_items'));
$query1->where($db1->quoteName('id')." = ".$db1->quote($idAnterior));
$db1->setQuery($query1);
$resultadoAnterior = $db1->loadResult();
?>
<?php
$idAtual = (( $this->item->id));
$db2 = JFactory::getDBO();
$query2 = $db2->getQuery(true);
$query2->select('created');
$query2->from($db2->quoteName('xnrhd_k2_items'));
$query2->where($db2->quoteName('id')." = ".$db2->quote($idAtual));
$db2->setQuery($query2);
$resultadoAtual = $db2->loadResult();
?>
<?php if(JHTML::_('date', $resultadoAnterior) == JHTML::_('date', $resultadoAtual)): ?>
<span>
</span>
<?php else: ?>
<span class="catItemDateCreated">
<?php echo JHTML::_('date', $this->item->created , JText::_('d M Y')) ?>
</span>
<?php endif; ?>
<?php endif ?>
</div>

If the date of the current item is equal to the new item, the date does not appear.

Please Log in or Create an account to join the conversation.

  • Mohamed Abdelaziz
  • Mohamed Abdelaziz's Avatar
  • Offline
  • Platinum Member
  • Joomla Developer
More
8 years 2 months ago #156424 by Mohamed Abdelaziz
Replied by Mohamed Abdelaziz on topic [solved] how to get the creation date of the previous item
Hello Wagner,

I think using the
Joomla\Utilities\ArrayHelper::pivot($source, $key)
will make the code shorter, more readable and more efficient.

Let's say you got the items as an array of objects ($items) which has created as a key for the creation date, simply you can use this statement to get an array of array of objects structured like you need
$itemsByDate = Joomla\Utilities\ArrayHelper::pivot($items, 'created');

Then, with 2 nested foreach loops you can achieve your goal, like this:
foreach($itemsByDate as $createdDate=>$dateItems){
	?>
	<span class="catItemDateCreated">
		<?php echo JHTML::_('date', $createdDate , JText::_('d M Y')) ?>
	</span>
	<?php
	foreach($dateItems as $item){
		//Insert here the code of $item display
	}
}

Multiple Extra Fields Groups for K2
AutoMeta for K2
Chained Fields for K2
More K2 Extensions In My Extensions Store

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum