Bug #78751 [Com]: serialize() converts DateTimeImmutable to DateTime when serialising DatePeriod
| From: | cory dot jacobsen at 7shifts dot com | Date: | Thu, 28 Nov 2019 20:47:40 +0000 |
| Subject: | Bug #78751 [Com]: serialize() converts DateTimeImmutable to DateTime when serialising DatePeriod | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-223924@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78751&edit=1
ID: 78751
Comment by: cory dot jacobsen at 7shifts dot com
Reported by: michael dot heerklotz at web dot de
Summary: serialize() converts DateTimeImmutable to DateTime
when serialising DatePeriod
Status: Closed
Type: Bug
Package: Date/time related
Operating System: Ubuntu
PHP Version: 7.3.11
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
This is not a backwards compatible change. Our company just updated from 7.3.11 to 7.3.12 and this
broke a lot of tests and production code. Not sure what we can do at this point...
Previous Comments:
------------------------------------------------------------------------
[2019-10-28 12:10:33] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=89c327f8848c1a56a61479ee5e7fdd3694d0f867
Log: Fix #78751: Serialising DatePeriod converts DateTimeImmutable
------------------------------------------------------------------------
[2019-10-25 12:46:10] cmb@php.net
The following pull request has been associated:
Patch Name: Fix #78751: Serialising DatePeriod converts DateTimeImmutable
On GitHub: https://github.com/php/php-src/pull/4859
Patch: https://github.com/php/php-src/pull/4859.patch
------------------------------------------------------------------------
[2019-10-25 12:15:54] cmb@php.net
Confirmed: <https://3v4l.org/WaCMr>. This is, however,
not
particularly related to serialization, but rather a general issue
regarding the get_properties handler, which unconditionally
creates $start and $end as DateTime instances[1].
[1] <https://github.com/php/php-src/blob/php-7.3.11/ext/date/php_date.c#L5157>
------------------------------------------------------------------------
[2019-10-25 12:14:17] michael dot heerklotz at web dot de
I just realised, that my "Expected result" is misleading.
So here is another example code:
Test script:
---------------
<?php
$oDay = new DateTimeImmutable('2019-10-25');
$oDateInterval = DateInterval::createFromDateString('1 day');
$oDays = new DatePeriod($oDay, $oDateInterval, $oDay->modify('+1 day'));
print 'Before serialize():' . PHP_EOL;
foreach ($oDays as $oDay) {
print $oDay->modify('+1 day')->format('Y-m-d') . PHP_EOL;
print $oDay->format('Y-m-d') . PHP_EOL;
}
print 'After serialize():' . PHP_EOL;
$oDaysSerialized = unserialize(serialize($oDays));
foreach ($oDaysSerialized as $oDay) {
print $oDay->modify('+1 day')->format('Y-m-d') . PHP_EOL;
print $oDay->format('Y-m-d') . PHP_EOL;
}
Expected result:
----------------
Before serialize():
2019-10-26
2019-10-25
After serialize():
2019-10-26
2019-10-25
Actual result:
--------------
Before serialize():
2019-10-26
2019-10-25
After serialize():
2019-10-26
2019-10-26
------------------------------------------------------------------------
[2019-10-25 11:32:48] michael dot heerklotz at web dot de
Description:
------------
When serialising (serialize()) a DatePeriod object which has been initialised with DateTimeImmutable
instances, the DateTimeImmutable type gets lost, and is converted to DateTime
Test script:
---------------
<?php
$oDay = new DateTimeImmutable('2019-10-25');
$oDateInterval = DateInterval::createFromDateString('1 day');
$oDays = new DatePeriod($oDay, $oDateInterval, $oDay->modify('+1 day'));
print serialize($oDays);
// Output: O:10:"DatePeriod":6:{s:5:"start";O:8:"DateTime": ...
Expected result:
----------------
O:10:"DatePeriod":6:{s:5:"start";O:8:"DateTimeImmutable":3:{s:4:"date";s:26:"2019-10-25
00:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:10:"US/Pacific";}s:7:"current";N;s:3:"end";O:8:"DateTimeImmutable":3:{s:4:"date";s:26:"2019-10-26
00:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:10:"US/Pacific";}s:8:"interval";O:12:"DateInterval":16:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:1;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:7:"weekday";i:0;s:16:"weekday_behavior";i:0;s:17:"first_last_day_of";i:0;s:6:"invert";i:0;s:4:"days";b:0;s:12:"special_type";i:0;s:14:"special_amount";i:0;s:21:"have_weekday_relative";i:0;s:21:"have_special_relative";i:0;}s:11:"recurrences";i:1;s:18:"include_start_date";b:1;}2019-10-25
Actual result:
--------------
O:10:"DatePeriod":6:{s:5:"start";O:8:"DateTime":3:{s:4:"date";s:26:"2019-10-25
00:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:10:"US/Pacific";}s:7:"current";N;s:3:"end";O:8:"DateTime":3:{s:4:"date";s:26:"2019-10-26
00:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:10:"US/Pacific";}s:8:"interval";O:12:"DateInterval":16:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:1;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:7:"weekday";i:0;s:16:"weekday_behavior";i:0;s:17:"first_last_day_of";i:0;s:6:"invert";i:0;s:4:"days";b:0;s:12:"special_type";i:0;s:14:"special_amount";i:0;s:21:"have_weekday_relative";i:0;s:21:"have_special_relative";i:0;}s:11:"recurrences";i:1;s:18:"include_start_date";b:1;}2019-10-25
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78751&edit=1