Skip to content

Instantly share code, notes, and snippets.

@instabledesign
Last active August 5, 2019 18:35
Show Gist options
  • Save instabledesign/5c0020334b2893136cbd7e11fb5e4fb7 to your computer and use it in GitHub Desktop.
Save instabledesign/5c0020334b2893136cbd7e11fb5e4fb7 to your computer and use it in GitHub Desktop.
[Doctrine] Convert the DateTime to store it in UTC DateTime.

Doctrine UTCDateTimeType

Symfony

To override the default Doctrine\DBAL\Types\DateTimeType :

  • use the config.yml
  • you can enable it only when you need it with
Type::overrideType('datetime', 'AppBundle\Doctrine\DBAL\Types\UTCDateTimeType');

If you need to create a DateTime and you dont want to autoconvert to UTC you can instantiate new DateTimeNotConvertable it only skip the DBAL convertion

doctrine:
dbal:
types:
utcdatetime:
name: datetime
class: AppBundle\Doctrine\DBAL\Types\UTCDateTimeType
#src/AppBundle/Doctrine/DBAL/DateTimeNotConvertable.php
<?php
namespace AppBundle\Doctrine\DBAL;
/**
* Class DateTimeNotConvertable
* This class was use to skip the automatic DBAL DateTimeUTC transformation
* see AppBundle\Doctrine\DBAL\Types\UTCDateTimeType
*/
class DateTimeNotConvertable extends \DateTime
{
}
#src/AppBundle/Doctrine/DBAL/Types/UTCDateTimeType.php
<?php
namespace AppBundle\Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeType;
use AppBundle\Doctrine\DBAL\DateTimeNotConvertable;
class UTCDateTimeType extends DateTimeType
{
/**
* @var \DateTimezone
*/
private static $utcDateTimezone;
/**
* @var \DateTimeZone
*/
private static $serverDateTimezone;
/**
* @param mixed $value
* @param AbstractPlatform $platform
*
* @return mixed
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return $value;
}
if (!$value instanceof DateTimeNotConvertable) {
self::$utcDateTimezone = self::$utcDateTimezone ?: new \DateTimeZone('UTC');
if ('UTC' !== $value->getTimezone()->getName()) {
$value->setTimezone(self::$utcDateTimezone);
}
}
return $value->format($platform->getDateTimeFormatString());
}
/**
* @param mixed $value
* @param AbstractPlatform $platform
*
* @return $this|mixed
* @throws ConversionException
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return $value;
}
self::$serverDateTimezone = self::$serverDateTimezone ?: new \DateTimeZone(date_default_timezone_get());
if ($value instanceof \DateTime) {
if (self::$serverDateTimezone->getName() !== $value->getTimezone()->getName()) {
$value->setTimezone(self::$serverDateTimezone);
}
return $value;
}
self::$utcDateTimezone = self::$utcDateTimezone ?: new \DateTimeZone('UTC');
$converted = \DateTime::createFromFormat(
$platform->getDateTimeFormatString(),
$value,
self::$utcDateTimezone
);
if (!$converted) {
throw ConversionException::conversionFailedFormat(
$value,
$this->getName(),
$platform->getDateTimeFormatString()
);
}
return $converted->setTimezone(self::$serverDateTimezone);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment