1

In the database, I have following date: 1924-01-17, as a date-type, but when I render it as {{ person.BirthDate | date("d/m/Y") }}, it gives as a result 17/01/2024.

How can I solve the Year-problem?

WeSt
  • 2,628
  • 5
  • 22
  • 37
Lilly
  • 71
  • 1
  • 2
  • 13
  • I'm fairly certain it's a formatting issue, have you tried to convert it into a unixtimestamp and then try to display the date through the date("d/m/Y") ? – Epodax Jan 19 '15 at 08:35
  • 2
    "have you tried to convert it into a unixtimestamp" --- unix timestamp era starts in year 1970 – zerkms Jan 19 '15 at 08:36
  • @zerkms Ah yes of course, my mind is still stuck in the morning traffic. – Epodax Jan 19 '15 at 08:39
  • This is your question http://stackoverflow.com/questions/2871264/using-strtotime-for-dates-before-1970 – divaka Jan 19 '15 at 08:40
  • 1
    @divaka is it? "The date filter accepts strings (it must be in a format supported by the strtotime function), DateTime instances, or DateInterval instances.", I guess DateTime instances could also be used here. – eis Jan 19 '15 at 08:41
  • 1
    DateTime instance works fine... Thanks! – Lilly Jan 19 '15 at 08:46
  • @eis You are right. I've rushed a little bit :) Referring this question http://stackoverflow.com/questions/10792246/twig-date-filter-not-working-with-datetime-object "`date` is a built-in filter, you need not register it, especially not to the php `date` function, which cannot handle DateTime objects". So indeed DateTime instances will do trick. – divaka Jan 19 '15 at 08:58
  • @user2365583 great! added it as an answer. – eis Jan 19 '15 at 09:50
  • 1
    @zerkms most libs will manage negative timestamps – Alain Tiemblo Jan 19 '15 at 14:07

1 Answers1

2

Twig documentation for date says:

The date filter accepts strings (it must be in a format supported by the strtotime function), DateTime instances, or DateInterval instances.

DateTime instance should do the trick, and based on comments it fixed this.

eis
  • 51,991
  • 13
  • 150
  • 199