Get the Last Modified Date

I recently had to acquire the last modified date of a parents child pages. Now usually if you are on the page in question you would use something like this:
{modified_date format="%A %d-%b-%y %T %Z"}
But that doesn’t get pulled through when using something like CGSimpleSmarty to get child pages.


So I had to come up with another way to get the last modified date. This was important for the client because they wanted to show a case studies list with a small ribbon saying "New" when a page was less than 7 days old.

Lets Create the UDT

$alias = (!empty($params['alias']) ? $params['alias'] : false);
$assign = (!empty($params['assign']) ? $params['assign'] : false);
// If alias is set
if (!empty($alias)) { 
 // Load defaults
 global $gCms;
 $contentOps =& $gCms->GetContentOperations();
 $smarty =& $gCms->GetSmarty();
 // If content operations could be loaded
 if (!empty($contentOps)) {
  // Set second parameter to false if you want inactive pages to be shown
  $content = $contentOps->LoadContentFromAlias($alias,true);
  // Retrieve the last modified timestamp
  $timestamp = $content->GetModifiedDate();
  // If assign is set, then set smarty variable otherwise just return the timestamp
  if ($assign) $smarty->assign($assign, $timestamp);
  else   return $timestamp;

This UDT is actually pretty simple; it initially obtains the variables $alias and $assign and checks to see if they have been passed through. If $alias hasn’t been (since it is required) the UDT will skip the processes. $assign is less important so it is only checked further down the UDT.

If $alias exists then the UDT sets up some standard CMSMS functions and uses the $alias provided to get the pages content. It will then acquire the last modified date from the retrieved content and depending on whether $assign is set; it will either output to a smarty variable or just return the value found.

For the sake of this tutorial I have saved this UDT as "last_modified".

Applying it to the Template 

We now return to the template and for this use case I will be using the following code:

{if count($children)}
 <ul class="casestudies">
  {foreach from=$children item='child' name=study}
   {if $child.show_in_menu}
    {last_modified alias=$child.alias assign="timestamp"}
    {assign var="timestamp" value=$timestamp}
    {assign var="nowstamp" value=$}
    {math equation="x - y" x=$nowstamp y=$timestamp assign="dateDiff"}

    <li{if $} class="first"{elseif $} class="last"{/if}>
     {if $dateDiff < 604800}<span class="ribbon"></span>{/if}
     <a href="{cms_selflink href=$child.alias}" title="{$child.title}"><span>{$child.title}</span></a>
    </li>    {/if}   {/foreach}  </ul> {/if} {/literal}

To explain in better detail; the code above uses our newly created UDT to get the $child.alias's last modified date and gets the current timestamp from smarty using the $ function.

NOTE: Now if you are just trying to get the last modified date to display, you can ignore the $ bit and just use our UDT. Then call the variable you have assigned and format using the date_format modifier: 

{$timestamp|date_format:”%A, %B %e, %Y”}
(Output: Saturday, September 21, 2019)

The next bit of code uses Smarty’s maths function to get the difference between the timestamps, this is important to calculate how many days have gone by. The calculated value will be in seconds, so you can use this to get the hours, days or years if you wish. Google even has a converter built in to do it for you; just type “seconds to hours” for instance in Google and it will appear.

The final piece is to then make the ribbon appear if the total number of seconds is less than 7 days’ worth:

{if $dateDiff < 604800}<span class="ribbon"></span>{/if}

So in this example 604800 = 7 days which is exactly what we needed.

You should now be having the last modified date being used in your template and if you followed this all the way through, the ribbon will be showing up for every entry less than 7 days old. 

NOTE: This UDT could easily be changed into a last_modified plugin instead; if you want to do this but need some help get in touch with me and I will aid you where I can.