如何使用TYPO3 extbase流体正确输出DateTime [英] How to properly output DateTime with TYPO3 extbase fluid

查看:157
本文介绍了如何使用TYPO3 extbase流体正确输出DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个dateTime对象存储在数据库中:

I got two dateTime Objects stored in the Database:


  • 2014-11-03 09:00:00

  • 2014-10-21 13:45:00

当我尝试使用ViewHelper格式输出它们。日期

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

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

我得到以下结果:


  • 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?

推荐答案

确保数据库中的所有日期都处于相同的时区,因为那个信息没有保存。当您从外部API调用收到某些对象时,它们将在日期字符串中显示时区,通常为UTC。从您的内部呼叫,所有\DateTime对象将默认使用您的服务器默认时区。所以设置时区,然后保存到数据库:

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()));

将时区设置为服务器默认值很方便,因为不需要更改更改..但最好保存在'UTC'我想。在这种情况下,您需要在显示之前将其转换回服务器/用户时区。它可以在ViewHelper中完成(不是来自Typo3.Fluid的默认设置,但您可以轻松地将其扩展到包中 - 再次克隆并设置时区)。也许现在可以在流程中使用原则扩展,并保存时区与日期到数据库..我试过一年前,不能使它..

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..

这篇关于如何使用TYPO3 extbase流体正确输出DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆