php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78751 serialize() converts DateTimeImmutable to DateTime when serialising DatePeriod
Submitted: 2019-10-25 11:32 UTC Modified: 2019-11-28 22:27 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: michael dot heerklotz at web dot de Assigned: cmb (profile)
Status: Closed Package: Date/time related
PHP Version: 7.3.11 OS: Ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: michael dot heerklotz at web dot de
New email:
PHP Version: OS:

 

 [2019-10-25 11:32 UTC] 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

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-10-25 12:14 UTC] 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 12:15 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2019-10-25 12:15 UTC] 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:46 UTC] 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-28 12:10 UTC] 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-28 12:10 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2019-10-28 12:11 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2019-11-28 20:47 UTC] cory dot jacobsen at 7shifts dot com
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...
 [2019-11-28 22:27 UTC] cmb@php.net
Few bug fixes are backwards compatible, and in my opinion, the
former behavior was a bug.  Anyhow, if you feel this commit should
be reverted for PHP 7.3, I'm not strongly opposed, but would
suggest to discuss this on the internals mailing list.

[1] <https://www.php.net/mailing-lists.php#internals>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Nov 11 07:01:30 2024 UTC