php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42351 Unable to create dates with years <= -10000 and >= 10000
Submitted: 2007-08-20 16:17 UTC Modified: 2019-10-15 19:02 UTC
Votes:12
Avg. Score:4.1 ± 1.0
Reproduced:10 of 10 (100.0%)
Same Version:5 (50.0%)
Same OS:3 (30.0%)
From: mattsch at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.6.4 OS: Gentoo Linux 13.0
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
1 + 47 = ?
Subscribe to this entry?

 
 [2007-08-20 16:17 UTC] mattsch at gmail dot com
Description:
------------
I am unable to create dates with years <= -10000 and >= 10000.  ISO 8601:2004E allows for this under Expanded representations:

http://isotc.iso.org/livelink/livelink/4021199/ISO_8601_2004_E.zip?func=doc.Fetch&nodeid=4021199

ISO 8601:2004(E)
14 ? ISO 2004 ? All rights reserved
4.1.2.4 Expanded representations
If, by agreement, expanded representations are used, the formats shall be as specified below. The
interchange parties shall agree the additional number of digits in the time element year. In the examples below
it has been agreed to expand the time element year with two digits.
a) A specific day
Basic format: ?YYYYYMMDD Example: +0019850412
Extended format: ?YYYYY-MM-DD Example: +001985-04-12
b) A specific month
Basic format: ?YYYYY-MM Example: +001985-04
Extended format: not applicable
c) A specific year
Basic format: ?YYYYY Example: +001985
Extended format: not applicable
d) A specific century
Basic format: ?YYY Example: +0019
Extended format: not applicable

Reproduce code:
---------------
Negative year:

<?php
$date = new DateTime('-10000-01-01 00:00:00');
echo $date->format('Y-m-d H:i:s');
?>

Positive year:

<?php
$date = new DateTime('10000-01-01 00:00:00');
echo $date->format('Y-m-d H:i:s');
?>

Expected result:
----------------
Negative year outputs:

-10000-01-01 00:00:00

Positive year outputs:

10000-01-01 00:00:00

Actual result:
--------------
Negative year outputs:

2000-01-01 00:00:00

Positive year outputs:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (10000-01-01 00:00:00) at position 12 (0): Double time specification' in /test.php:5
Stack trace:
#0 /test.php(5): DateTime->__construct('10000-01-01 00:...')
#1 {main}
  thrown in /test.php on line 5

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-03 12:37 UTC] jani@php.net
Assigned to the ext/date maintainer.
 [2010-12-20 14:23 UTC] jani@php.net
-Package: Feature/Change Request +Package: Date/time related
 [2014-01-25 14:27 UTC] derick@php.net
-Status: Assigned +Status: Feedback
 [2014-01-25 14:27 UTC] derick@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.


Please mention when/how you want to create those dates?
 [2014-01-26 03:44 UTC] mattsch at gmail dot com
Well if you didn't delete my original message, you wouldn't be changing the status to feedback eh?  Funny enough, my original message was preserved when your system sent me an email saying you don't have enough information.  As originally stated, ISO dates should support years in this range and such years are used by astronomers. 


------------------------------------------------------------------------
[2007-08-20 16:17:31] mattsch at gmail dot com

Description:
------------
I am unable to create dates with years <= -10000 and >= 10000.  ISO 8601:2004E allows for this under Expanded representations:

http://isotc.iso.org/livelink/livelink/4021199/ISO_8601_2004_E.zip?func=doc.Fetch&nodeid=4021199

ISO 8601:2004(E)
14 © ISO 2004 – All rights reserved
4.1.2.4 Expanded representations
If, by agreement, expanded representations are used, the formats shall be as specified below. The
interchange parties shall agree the additional number of digits in the time element year. In the examples below
it has been agreed to expand the time element year with two digits.
a) A specific day
Basic format: ±YYYYYMMDD Example: +0019850412
Extended format: ±YYYYY-MM-DD Example: +001985-04-12
b) A specific month
Basic format: ±YYYYY-MM Example: +001985-04
Extended format: not applicable
c) A specific year
Basic format: ±YYYYY Example: +001985
Extended format: not applicable
d) A specific century
Basic format: ±YYY Example: +0019
Extended format: not applicable

Reproduce code:
---------------
Negative year:

<?php
$date = new DateTime('-10000-01-01 00:00:00');
echo $date->format('Y-m-d H:i:s');
?>

Positive year:

<?php
$date = new DateTime('10000-01-01 00:00:00');
echo $date->format('Y-m-d H:i:s');
?>

Expected result:
----------------
Negative year outputs:

-10000-01-01 00:00:00

Positive year outputs:

10000-01-01 00:00:00

Actual result:
--------------
Negative year outputs:

2000-01-01 00:00:00

Positive year outputs:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (10000-01-01 00:00:00) at position 12 (0): Double time specification' in /test.php:5
Stack trace:
#0 /test.php(5): DateTime->__construct('10000-01-01 00:...')
#1 {main}
  thrown in /test.php on line 5
 [2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2014-12-30 15:11 UTC] mattsch at gmail dot com
-Status: No Feedback +Status: Closed -Operating System: Gentoo Linux 2007.0 +Operating System: Gentoo Linux 13.0 -PHP Version: 5.2.4 +PHP Version: 5.6.4
 [2014-12-30 15:11 UTC] mattsch at gmail dot com
What are you talking about?  I've given you all the information you need to reproduce the bug and the reason why it's a bug.  Why are you closing this case?  Note that I just retested with the latest version of php and it is still not fixed yet.
 [2014-12-30 15:12 UTC] mattsch at gmail dot com
-Summary: Unable to create dates with years <= -10000 and >= 10000 +Summary: Unable to create dates with years <= -10000 and7e68099f >= 10000 -Status: Closed +Status: Assigned
 [2014-12-30 15:12 UTC] mattsch at gmail dot com
I wasn't able to change the status to re-opened.  Please reopen.
 [2014-12-30 15:13 UTC] mattsch at gmail dot com
-Summary: Unable to create dates with years <= -10000 and7e68099f >= 10000 +Summary: Unable to create dates with years <= -10000 and >= 10000
 [2014-12-30 15:13 UTC] mattsch at gmail dot com
fixed summary
 [2017-10-24 08:14 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: derick +Assigned To:
 [2019-02-20 21:36 UTC] icyd3mon at gmail dot com
This still persists in PHP 7.2.9.
I think the perpetrator lies in https://github.com/php/php-src/blob/653e47da1690ee91fb62488944d43cf70cbbd0a8/ext/date/lib/parse_date.c#L25252

Where it is assumed that the maximum length of year is four digits.

Effectively, it could be 8 digits, since 
<?php 
$date = new \DateTime('@'.PHP_INT_MAX);
echo $date->format('c');
?>
 
returns "219250468-12-04T15:30:07+00:00"
 [2019-10-15 19:02 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2019-10-15 19:02 UTC] derick@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at
http://www.php.net/downloads.php

This fix will be in PHP 8, as it forms part of the master branch of timelib.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 17:01:30 2024 UTC