api.emotionhero.com/src/Tools/UTCDateTimeType.php
2016-09-01 11:15:17 +01:00

45 lines
No EOL
1.1 KiB
PHP

<?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;
}
}