Req #60302 [Asn]: DateTime::createFromFormat should new static(), not new self()

[email protected]
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=60302&edit=1

 ID:                 60302
 Updated by:         [email protected]
 Reported by:        kontakt at beberlei dot de
 Summary:            DateTime::createFromFormat should new static(), not
                     new self()
 Status:             Assigned
 Type:               Feature/Change Request
 Package:            Date/time related
 PHP Version:        Irrelevant
 Assigned To:        derick
 Block user comment: N
 Private report:     N

 New Comment:

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…


Previous Comments:
------------------------------------------------------------------------
[2012-05-14 10:09:57] [email protected]

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());
    }

------------------------------------------------------------------------
[2011-11-14 21:51:54] [email protected]

Had a quick look at this, but it wasn't as simple as I thought. Needs some more investigation.

------------------------------------------------------------------------
[2011-11-14 20:00:54] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=60302&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.