api.emotionhero.com/src/Tools/UTCDateTimeType.php

45 lines
1.1 KiB
PHP
Raw Normal View History

2016-09-01 10:15:17 +00:00
<?php
namespace EmotionHero\Tools;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
class UTCDateTimeType extends DateTimeType
{
static private $utc = null;
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
if (is_null(self::$utc)) {
self::$utc = new \DateTimeZone('UTC');
}
$value->setTimeZone(self::$utc);
return $value->format($platform->getDateTimeFormatString());
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
if (is_null(self::$utc)) {
self::$utc = new \DateTimeZone('UTC');
}
$val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::$utc);
if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
}
}