Fox
Fox

Reputation: 633

How can I avoid datetime problems in TYPO3?

I have created a little extension which uses datetimes to view some specific events (event date and event time), but I always run into trouble if I try to get the correct datetime from database to frontend.

I can set the datetime for each event via TYPO3 backend:

enter image description here

But if I try to get this value on frontend like:

<f:format.date format="d.m.Y - H:i:s">{event.appointment}</f:format.date>

Then the output is not "10.04.2016 - 08:00:00" it is "10.04.2016 - 10:00:00".

How can I avoid this behaviour? I have set the timezone via install tool too:

[SYS][phpTimeZone] = Europe/Berlin

but I get always the wrong datetime. If I try something like this:

DebugUtility::debug(new \DateTime());

then I get the correct output:

{"date":"2016-04-09 20:23:38.000000","timezone_type":3,"timezone":"Europe\/Berlin"}

If I take a look at the database I can see that the correct datetime is stored:

enter image description here

So I don't know why I got the wrong datetime, any suggestions?

Upvotes: 4

Views: 1154

Answers (1)

biesior
biesior

Reputation: 55798

There is some inconsistency with date converting within TYPO3 it behaves different in TCE and in Extbase. Extbase considers that all dates are stored in DB in UTC therefore at data mapping process converts your date from UTC to theoretically local time (IMHO that shouldn't be done or should be configurable via Install Tool), the dirty but working trick is fooling TYPO3, just in the Install Tool > All configurations find the [SYS][phpTimeZone] setting and set its value to UTC.

That way Extbase will think that you're within UTC zone and you won't need to change anything in the php.ini.

The other solution is storing dates in DB as timestamps.

Upvotes: 7

Related Questions