|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 09:00:01 2025 UTC | 
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()); }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…