VarDateTimeType.php 901 Bytes
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Types;

5
use DateTime;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7

8
use function date_create;
9 10

/**
Benjamin Morel's avatar
Benjamin Morel committed
11
 * Variable DateTime Type using date_create() instead of DateTime::createFromFormat().
12 13 14 15 16 17 18 19
 *
 * This type has performance implications as it runs twice as long as the regular
 * {@see DateTimeType}, however in certain PostgreSQL configurations with
 * TIMESTAMP(n) columns where n > 0 it is necessary to use this type.
 */
class VarDateTimeType extends DateTimeType
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
20
     * {@inheritdoc}
21 22 23
     */
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
24
        if ($value === null || $value instanceof DateTime) {
25
            return $value;
26 27 28
        }

        $val = date_create($value);
29
        if ($val === false) {
30 31
            throw ConversionException::conversionFailed($value, $this->getName());
        }
32

33 34
        return $val;
    }
35
}