Changing date formats in Contao news templates

Posted by Howard Richardson (comments: 1)

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

Add a comment

Comment by Ernest McDermon |

Thanks for the tip, I'm a Contao Developer in the US and appreciate the help this article provided on modifying the date entry for the Contao News list. Contao documentation in English is not all that great and results in a lot of digging for info or experimentation to get it right.

Ernest McDermon, Snellville GA USA