Search code examples
phpsmarty

Smarty Template date_format gives unpredictable results


I am passing a timestamp into a Smarty Template, and using date_format gives unpredictable results. Here's an example template that demonstrates the problem most elegantly:

{"1456602208"}
{"1456602208"|date_format}
{"1432808316"}
{"1432808316"|date_format}

The output of this template:

1456602208
Oct 19, 2208
1432808316
May 28, 2015

The second date is correctly formatted. The first one, however, should be

Feb 27, 2016

Why is this?


Solution

  • The best way to avoid this is to cast to int the timestamp. Now it's a numeric string.

    Smarty did some changes in 2.6.10 to use all numeric input values as timestamp, but before was not always the case.

    As I can see the first timestamp ends in 2208 and also the year returned for that timestamp is 2208, so I think smarty think it's not a timestamp, but a date formatted somehow.

    See more here (smarty doc)