Changing date formats in Contao news templates

2011-07-13 13:22 by Howard (comments: 0)

Something I often need to do is change the date format from the basic numeric format to something a bit more user-friendly. It's long been a bit of an omission in Contao that you can't do this easily, and trying to track down the exact PHP code and variables to change is a pain.

Something you can do in any template to see which variables are available to you is:

<pre><?php $this->showTemplateVars(); ?></pre>

 

This will output the whole templates available variables. In the case of news_latest.tpl you'll see the variable we'll need to work with is simply called date.

Originally this variable will be in the numerical format specified in your Contao settings. So long as this is one of the standard formats acceptable by the PHP function strtotime (and it should be) you can go from this numerical string date to a UNIX timestamp, and from there to a date format of your choice like this:


<?php echo $this->parseDate("Your string format here!!",strtotime($this->date)); ?>

Usually I'll use a string format something like:
j M Y = 1 Aug 2011
There are plenty of other options and examples on the PHP website.

Go back