Textpattern tips, tutorials and code snippets

Display the published and modified dates

This tip will display the published date and, only if article has been changed, the last modified date article. There are two methods here, both of which require some PHP.

Method #1

You can place the following code in within your article form where you want to display the dates, or alternatively directly into your page template.

<p class="published"> By <txp:author /> on <txp:posted /> <txp:php> $changed = modified(array( 'format'=>'%m %d %Y – %H:%I')); echo $changed == posted(array('format'=>'%m %d %Y - %H:%I')) ? '' : '| Modified on '.$changed.' by '.safe_field("RealName", "txp_users", "name='".safe_field("LastModID", "textpattern", "ID='".$thisarticle['thisid']."'")."'");</txp:php>

Method #2

This method, based on a contribution by Els Lepelaars in this thread, uses Textpattern’s in-built if_variable tag:

<p class="published">Published by <txp:author /> on <txp:posted format="%m %d %Y - %H:%I" /> <txp:variable name="modified" value='<txp:modified />' />
<txp:if_variable name="modified" value='<txp:posted />'>
	<!-- display nothing -->
<txp:else />
| Modified on <txp:modified /> by <txp:php>echo safe_field("RealName", "txp_users", "name='".safe_field("LastModID", "textpattern", "ID='".$thisarticle['thisid']."'")."'");</txp:php>
</txp:if_variable></p>

As you can see above, there are 2 possibilities. Each one works fine for the same purpose.

2 Comments Comment feed

This tip is an answer to Tye from this original thread .
When I wrote this code, I noticed a problem with inequal dates between published and modified ones. This problem, reported by Rick Silletti , had been corrected by Wet now.
So, notice you’ll don’t need a format date attribute for the next TXP 4.0.9 version if you want to use the preferences date format choosen.

The new simplified tip will be as this :

<p class="published">Published by <txp:author /> on <txp:posted /> <txp:php> $changed = modified(array()); echo $changed == posted(array()) ? '' : '| Modified by '.safe_field("RealName", "txp_users", "name='".safe_field("LastModID", "textpattern", "ID='".$thisarticle['thisid']."'")."'").' on '.modified(array()).'';</txp:php>

Is there any way to use something like this in the footer instead of within the article?

Add a comment

Use Textile help to style your comments