php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60302 DateTime::createFromFormat should new static(), not new self()
Submitted: 2011-11-14 20:00 UTC Modified: 2020-08-02 23:44 UTC
Votes:13
Avg. Score:4.2 ± 0.9
Reproduced:12 of 12 (100.0%)
Same Version:1 (8.3%)
Same OS:0 (0.0%)
From: kontakt at beberlei dot de Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2011-11-14 20:00 UTC] kontakt at beberlei dot de
Description:
------------
I want to extend DateTime, but without DateTime::createFromFormat, actually creating instances from my class, i.e. "MyDateTime::createFromFormat" this is rather problematic.



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

class MyDateTime extends DateTime { }

$d = MyDateTime::createFromFormat('Y-m-d', '2011-01-01');

echo get_class($d);


Expected result:
----------------
MyDateTime

Actual result:
--------------
DateTime

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-14 21:51 UTC] derick@php.net
Had a quick look at this, but it wasn't as simple as I thought. Needs some more investigation.
 [2011-11-14 21:51 UTC] derick@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: derick
 [2012-05-14 10:09 UTC] seld@php.net
I added this in my subclass. Works well as a workaround, but it would be nice to have it in code indeed.

    public static function createFromFormat($a, $b, $c = null) 
    {
        $date = $c ? parent::createFromFormat($a, $b, $c) : parent::createFromFormat($a, $b);

        return new static('@'.$date->format('U'), $date->getTimeZone());
    }
 [2017-03-24 06:59 UTC] heiglandreas@php.net
Hey @seld - I hope you fixed that code-snippet to this:

    public static function createFromFormat($a, $b, $c = null) 
    {
        $date = $c ? parent::createFromFormat($a, $b, $c) : parent::createFromFormat($a, $b);

        $newDate = new static('@'.$date->format('U'));
        $newDate->setTimezone($date->getTimezone());

        return $newDate;
    }


Before the timezone was not set due to the fact that the timezone is ignored when the first parameter contains a timezone or an offset. And a timestamp always contains the timezone UTC by definition…
 [2020-08-02 23:43 UTC] derick@php.net
Automatic comment on behalf of github@derickrethans.nl
Revision: http://git.php.net/?p=php-src.git;a=commit;h=90434d7fe3ac204b56b85cf8ca606e7dd8cf27ac
Log: Fixed bug #60302: DateTime::createFromFormat should new static(), not new self()
 [2020-08-02 23:44 UTC] derick@php.net
-Status: Assigned +Status: Closed
 [2020-08-02 23:44 UTC] derick@php.net
-Type: Feature/Change Request +Type: Bug
 [2020-08-02 23:44 UTC] derick@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 02 20:01:32 2024 UTC