How to properly output DateTime with TYPO3 extbase

2019-07-04 17:35发布

I got two dateTime Objects stored in the Database:

  • 2014-11-03 09:00:00
  • 2014-10-21 13:45:00

When i try to output them with the ViewHelper format.date

<f:format.date format="H:i">{termin.datumBeginn}</f:format.date>

I get the following results:

  • 10:00
  • 15:45

So i got a one hour shift and a two hour shift which i can't write a workaround for. How do i set the timezones properly to have a clean output?

4条回答
Viruses.
2楼-- · 2019-07-04 17:38

To solve this issue you need to set $TYPO3_CONF_VARS['SYS']['phpTimeZone'] as "UTC" and $TYPO3_CONF_VARS['SYS']['serverTimeZone'] as "0" (probably only first setting will be enough). You can do it through typo3 backend, using Install tool. If you have domain model you can use a workaround:

/**
 * Returns the startDate
 *
 * @return \DateTime|NULL $startDate
 */
public function getStartDate() {
    $date = $this->startDate;
    if($date) {
        $date->setTimezone(new \DateTimeZone('UTC'));
    }
    return $date;
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-07-04 17:40

Please make sure that you have put setting the Timezone for PHP in the php.ini file

Example: date.timezone = "Asia/Phnom_Penh"

For more information of PHP Timezone please refer to this link: http://php.net/manual/en/timezones.america.php

查看更多
对你真心纯属浪费
4楼-- · 2019-07-04 17:44

Although this is very old, I want to highlight that this was a bug in Extbase until recently, and we fixed it in TYPO3 7.6 and 8. The dates are now properly read as UTC, as which they are stored in the database, and converted to your server timezone.

查看更多
对你真心纯属浪费
5楼-- · 2019-07-04 17:45

Ensure that all dates in you database are in same timezone, because that information is not saved there. When you receive some objects from external API calls, they will have timezone in date string and it will be usually UTC. From your internal calls all \DateTime objects will use by default your server default timezone. So set timezone before saving it to database:

$receivedDate = new \DateTime($date);
$reveivedDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));

Setting timezone to server default is convenient, because requires no more changes.. but it's better to save it in 'UTC' I think. In that case you will need to convert it back to your server/user timezone before showing it. It can be done in ViewHelper (not default one from Typo3.Fluid but you can easily extend it in your package - clone and set timezone again). Maybe it's possible now to use doctrine extensions in flow, and save timezone with date to database.. i tried it year ago and couldn't make it..

查看更多
登录 后发表回答