-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtcDateType.php
54 lines (44 loc) · 1.37 KB
/
UtcDateType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
declare(strict_types=1);
/*
* This file is part of the DoctrineUTCDateTime component package.
*
* (c) Viktor Linkin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Linkin\Component\DoctrineUTCDateTime\DBAL\Types;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DateType;
/**
* @author Viktor Linkin <[email protected]>
*/
class UtcDateType extends DateType
{
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($date, AbstractPlatform $platform): ?string
{
if ($date instanceof DateTimeInterface) {
return parent::convertToDatabaseValue($date->setTimezone(new DateTimeZone('UTC')), $platform);
}
return parent::convertToDatabaseValue($date, $platform);
}
/**
* {@inheritdoc}
*/
public function convertToPHPValue($date, AbstractPlatform $platform): ?DateTimeInterface
{
$phpDate = parent::convertToPHPValue($date, $platform);
if (null === $phpDate) {
return null;
}
$formatString = $platform->getDateFormatString();
return DateTime::createFromFormat('!'.$formatString, $phpDate->format($formatString), new DateTimeZone('UTC'));
}
}