Search code examples
phpcodeigniterexpressionengine

UNIX timestamp of the last time an entry was edited


In Expression Engine the {edit_date} tag would not return a UNIX timestamp like {entry_date} does. To work around that I used the approach below.

Does any one have an idea of how this could be made easier?

{exp:channel:entries channel="pieces" limit="1" track_views="three"}
<?php
$edit_date_string = {edit_date};
$edit_date = NULL;
if(!isset($edit_date_string)) {
  $edit_date = {entry_date};
} else {
  // Date format: 2011 05 25 00:53:44
  // Raw: 20110525005344
  $hour   = substr($edit_date_string, -6, 2);
  $minute = substr($edit_date_string, -4, 2);
  $second = substr($edit_date_string, -2, 2);
  $day    = substr($edit_date_string, -8, 2);
  $month  = substr($edit_date_string, -10, 2);
  $year   = substr($edit_date_string, -12, 2);
  $edit_date = mktime($hour, $minute, $second, $month, $day, $year);
}
echo $edit_date;
?>
{/exp:channel:entries}

As a side note it seems this is an inconsistency which makes it harder to process and compare dates. There would probably be a good reason for it. Does anyone know? Thanks


Solution

  • If you've got PHP 5.3, there's DateTime::createFromFormat:

    $edit_date = DateTime::createFromFormat('YmdHis', $edit_date_string)->getTimestamp();
    

    Or, even more directly, according to the Expression Engine docs:

    $edit_date = {edit_date format="%U"}