php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71520 Adding the DateTime constants to the DateTimeInterface interface
Submitted: 2016-02-04 09:25 UTC Modified: 2017-06-02 22:57 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (100.0%)
From: nyamsprod at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Date/time related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
 [2016-02-04 09:25 UTC] nyamsprod at gmail dot com
Description:
------------
Currently it's not possible to access DateTime constants directly from DateTimeImmutable.


Test script:
---------------
$datetime = new DateTimeImmutable('2000-01-01 00:00:00', new DateTimeZone('Africa/Kinshasa'));

var_dump($datetime->format(DateTimeImmutable::ATOM));
var_dump($datetime->format(DateTimeImmutable::COOKIE));
var_dump($datetime->format(DateTimeImmutable::ISO8601));
var_dump($datetime->format(DateTimeImmutable::RFC822));
var_dump($datetime->format(DateTimeImmutable::RFC850));
var_dump($datetime->format(DateTimeImmutable::RFC1036));
var_dump($datetime->format(DateTimeImmutable::RFC2822));
var_dump($datetime->format(DateTimeImmutable::RFC3339));
var_dump($datetime->format(DateTimeImmutable::RSS));
var_dump($datetime->format(DateTimeImmutable::W3C));

Expected result:
----------------
string(25) "2000-01-01T00:00:00+01:00"
string(34) "Saturday, 01-Jan-2000 00:00:00 WAT"
string(24) "2000-01-01T00:00:00+0100"
string(29) "Sat, 01 Jan 00 00:00:00 +0100"
string(32) "Saturday, 01-Jan-00 00:00:00 WAT"
string(29) "Sat, 01 Jan 00 00:00:00 +0100"
string(31) "Sat, 01 Jan 2000 00:00:00 +0100"
string(25) "2000-01-01T00:00:00+01:00"
string(31) "Sat, 01 Jan 2000 00:00:00 +0100"
string(25) "2000-01-01T00:00:00+01:00"

Actual result:
--------------
The following constant are missing on the DateTimeInterface interface

DateTimeInterface::ATOM,
DateTimeInterface::COOKIE,
DateTimeInterface::ISO8601,
DateTimeInterface::RFC822,
DateTimeInterface::RFC850,
DateTimeInterface::RFC1036,
DateTimeInterface::RFC2822,
DateTimeInterface::RFC3339,
DateTimeInterface::RSS,
DateTimeInterface::W3C

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-04 09:33 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2016-02-04 09:33 UTC] requinix@php.net
They aren't missing. The constants are on the DateTime class. Why should they be copied onto DateTimeImmutable? What's wrong with keeping them on DateTime?
 [2016-02-04 10:14 UTC] nyamsprod at gmail dot com
-Status: Feedback +Status: Open
 [2016-02-04 10:14 UTC] nyamsprod at gmail dot com
I'm suggesting to have them on the DateTimeInterface because:

- they are used by the DateTimeInterface::format method
- they are defined in RFCs that won't change during the lifetime of any implementing class
- DateTimeImmutable is supposed to be the immutable replica of DateTime so that one could completely remove any reference to DateTime in his/her codebase when migrating from DateTime to DateTimeImmutable.
 [2016-02-07 10:20 UTC] nyamsprod at gmail dot com
Another argument in favor of this move is to forbidden constant value override in DateTimeInterface object subclasses

https://3v4l.org/iZEks
 [2017-04-17 17:36 UTC] bugs dot php dot net at majkl578 dot cz
Hello, I've submitted a PR for this change, see here: https://github.com/php/php-src/pull/2483.
 [2017-06-02 22:57 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC