In smarty, I have a date string formatted like this: DD/MM/YYYY
When I try to use date_format
it gets the date wrong.
How can I make it understand what the initial string is formatted like?
The documentation of date_format
Smarty variable modifier explains what it expects for the value to be formatted:
This formats a date and time into the given
strftime()
format. Dates can be passed to Smarty as unix timestamps, DateTime objects, mysql timestamps or any string made up of month day year, parsable by php'sstrtotime()
.
The date format you are using (DD/MM/YYYY
) is not recognized by strtotime()
and there is a good reason for it: for more that one third of the days of the year, this format cannot be told apart from MM/DD/YYYY
. The PHP developers had to choose which of these two formats to recognize and they have chosen MM/DD/YYYY
(probably because it has higher coverage).
A possible solution to your problem is to use the replace
modifier first, to change /
to -
or .
. Both DD-MM-YYYY
and DD.MM.YYYY
formats are valid and recognized by strtotime()
and they do not clash with other formats.
If your current code looks like:
{$date|date_format:'%e %B %Y'}
change it to:
{$date|replace:'/':'.'|date_format:'%e %B %Y'}