Date formatting in Smarty

If you use the so called, German date format (day of month. month. year) you may have wondered how to achieve it with Smarty – without the leading zeroes for month.

You all know the common way to format date in Smarty:

  1. {$myDate|date_format:"%e. %m. %Y"}

If you ever wanted to have month number without the leading zeroes, here’s how:

  1. {$myDate|date_format:"%e. %#m. %Y"}

or even a cooler way:

  1. {assign var="myDateTimestamp" value=$myDate|strtotime}
  2. {"j. n. Y"|date:$myDateTimestamp}

(because you can use almost any PHP function as Smarty variable modifier, just solve the parameters-order problem)

Edit 2011/10/25: Just found out that date_format:”%e. %#m. %Y” no longer works in one of my projects, I guess they moved them to a different server. Fortunately, there’s yet another solution: use a hyphen instead of a hash mark.

  1. {$myDate|date_format:"%e. %-m. %Y"}

Leave a Reply