php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80047 DatePeriod doesn't support custom DateTimeImmutable
Submitted: 2020-09-03 12:01 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: oognic at gmail dot com Assigned:
Status: Open Package: Date/time related
PHP Version: 7.2.33 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-09-03 12:01 UTC] oognic at gmail dot com
Description:
------------
If we create a custom class that inherits from DateTimeImmutable, this class can be given as parameter to DatePeriod.

However, when iterating or trying to get the start or end dates, DatePeriod will try to return instances of our custom class but without using its constructor, which will produce incoherent objects.

See this PR for more details: https://github.com/thecodingmachine/safe/pull/227/files

Test script:
---------------
<?php

class CustomDateTimeImmutable extends \DateTimeImmutable {

    private $foo;

    public function __construct($time = "now", $timezone = NULL)
    {
        parent::__construct($time, $timezone);
        $this->foo = "foo"; 
    }
    
    public function getFoo()
    {
        return $this->foo;
    }

}

$datePeriod = new \DatePeriod(new CustomDateTimeImmutable('2020-01-01'), new \DateInterval('P1D'), (new CustomDateTimeImmutable('2020-01-03'))->modify('+1 day'));

var_dump($datePeriod->getStartDate()->getFoo());

$strings = [];
foreach ($datePeriod as $date) {
    var_dump($date->getFoo());
}



Expected result:
----------------
"foo"
"foo"
"foo"
"foo"

Actual result:
--------------
NULL
NULL
NULL
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Oct 29 14:01:44 2020 UTC