Skip to content

Commit bace7ae

Browse files
committed
bug symfony#15163 Update DateTimeToArrayTransformer.php (zhil)
This PR was submitted for the 2.8 branch but it was merged into the 2.3 branch instead (closes symfony#15163). Discussion ---------- Update DateTimeToArrayTransformer.php | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | none php have annoying bug with timezone handling. Some timezones (like US/Eastern, US/Central, US/Mountain) are considered "not standard" and not parsed in some cases. For example, code ``` php -r '$d = new \DateTime("2015-07-01 16:11", new \DateTimeZone("US/Eastern")); print $d->format("r");' ``` return output ``` Wed, 01 Jul 2015 16:11:00 -0400 ``` However, code ``` php -r '$d = new \DateTime("2015-07-01 16:11 US/Eastern"); print $d->format("r");' ``` throw exception ``` Exception' with message 'DateTime::__construct(): Failed to parse time string (2015-07-01 16:11 US/Eastern) at position 17 (U): The timezone could not be found in the database' ``` Thats why timezone US/Eastern works in some cases and didnt work in other cases. This PR fix usage of US/Eastern in code like ``` $formBuilder->add("createdTimestamp", "datetime", ['view_timezone'=$user->timezone]) ``` Commits ------- 27b824a Update DateTimeToArrayTransformer.php
2 parents 2c0c232 + 27b824a commit bace7ae

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,16 @@ public function reverseTransform($value)
179179

180180
try {
181181
$dateTime = new \DateTime(sprintf(
182-
'%s-%s-%s %s:%s:%s %s',
182+
'%s-%s-%s %s:%s:%s',
183183
empty($value['year']) ? '1970' : $value['year'],
184184
empty($value['month']) ? '1' : $value['month'],
185185
empty($value['day']) ? '1' : $value['day'],
186186
empty($value['hour']) ? '0' : $value['hour'],
187187
empty($value['minute']) ? '0' : $value['minute'],
188-
empty($value['second']) ? '0' : $value['second'],
189-
$this->outputTimezone
190-
));
188+
empty($value['second']) ? '0' : $value['second']
189+
),
190+
new \DateTimeZone($this->outputTimezone)
191+
);
191192

192193
if ($this->inputTimezone !== $this->outputTimezone) {
193194
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));

0 commit comments

Comments
 (0)