[TYPO3-mvc] f:format.date timezone "problem"?

Xavier Perseguers typo3 at perseguers.ch
Wed Jul 28 16:31:40 CEST 2010


Hi Christian,

> I stumbled upon that recently, too.
>
> If you enter a date in the TYPO3 backend it will store the timestamp of
> midnight in the timezone you are in the database.

I find that it stores it in UTC, whatever timezone I define in 
localconf.php with $TYPO3_CONF_VARS['SYS']['serverTimeZone'].

> When using the @-syntax in DateTime UTC (=GMT) will be assumed as the
> timezone of that DateTime syntax. So if you use format() it will render
> the date assuming UTC timezone.

Exactly!

> To fix this you can either write a custom view helper that handles this
> or you let your object return DateTime-Objects that you configure
> correctly before.

Problem can be reproduced like that:

<?php
$unix = 1255125600;
$myDate = new DateTime('@' . $unix);

echo 'Unix: ' . $unix . "\n";
echo 'date: ' . date('d.m.Y H:i', $unix) . "\n";
echo 'DateTime: ' . $myDate->format('d.m.Y H:i') . "\n";

// Manually use an offset
$now = new DateTime();
$offset = $now->getOffset();

$myDate = new DateTime('@' . ($unix + $offset));
echo 'DateTime: ' . $myDate->format('d.m.Y H:i') . "\n";
?>

I added a new getter "getLocalDate()" which does this:

public function getLocalDate() {
	$now = new DateTime();
	$offset = $now->getOffset();

	return new DateTime('@' . ($this->date + $offset));
}

I hope this problem can help others too.

Xavier


More information about the TYPO3-project-typo3v4mvc mailing list