Bug #71693 [Opn->Nab]: Date Interval Subtraction for Leap Day
| From: | requinix@php.net | Date: | Tue, 01 Mar 2016 02:37:55 +0000 |
| Subject: | Bug #71693 [Opn->Nab]: Date Interval Subtraction for Leap Day | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-199541@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=71693&edit=1
ID: 71693
Updated by: requinix@php.net
Reported by: snyderpa at gmail dot com
Summary: Date Interval Subtraction for Leap Day
-Status: Open
+Status: Not a bug
Type: Bug
Package: Date/time related
Operating System: Linux
PHP Version: 5.5.32
Block user comment: N
Private report: N
New Comment:
->add and ->sub *modify* the object. Then they return the instance itself, not a new one.
Try code more like
$tomorrow = clone $today;
$tomorrow->add($oneDayInterval);
echo("\nTOMORROW\n");
var_export($tomorrow);
Previous Comments:
------------------------------------------------------------------------
[2016-03-01 00:47:24] snyderpa at gmail dot com
Also, $tomorrowMinus18 should be in year 1998, not 1980.
------------------------------------------------------------------------
[2016-03-01 00:09:42] snyderpa at gmail dot com
Description:
------------
I had a test begin to fail. I was naively expecting subtraction of an interval 'P18Y' to
yield a date on which all users born that day would turn 18 years old today. Although the leap day
should be an exception for my test case
(https://www.quora.com/If-youre-born-on-February-29th-when-do-you-legally-turn-one-year-older) I
noticed that there appears to be some brokenness with subtraction of 'P1D' interval from
the leap day, and the subsequent subtraction of the 'P18Y' interval from that result.
p.s. My Turing test asks for the solution to 50 - 32 -- spooky :-)
Test script:
---------------
<?php
/* get some significant dates */
$today = new DateTime();
echo("\nTODAY\n");
var_export($today);
$minorAgeInterval = new DateInterval('P18Y');
$oneDayInterval = new DateInterval('P1D');
$tomorrow = $today->add($oneDayInterval);
echo("\nTOMORROW\n");
var_export($tomorrow);
$yesterday = $today->sub($oneDayInterval);
echo("\nYESTERDAY\n");
var_export($yesterday);
$todayMinus18 = $today->sub($minorAgeInterval);
echo("\nTODAY MINUS 18\n");
var_export($todayMinus18);
$tomorrowMinus18 = $tomorrow->sub($minorAgeInterval);
echo("\nTOMORROW MINUS 18\n");
var_export($tomorrowMinus18);
$yesterdayMinus18 = $yesterday->sub($minorAgeInterval);
echo("\nYESTERDAY MINUS 18\n");
var_export($yesterdayMinus18);
?>
Expected result:
----------------
paul@paul-VirtualBox:~$ php php_date_fun.php
TODAY
DateTime::__set_state(array(
'date' => '2016-02-29 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
TOMORROW
DateTime::__set_state(array(
'date' => '2016-03-01 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
YESTERDAY
DateTime::__set_state(array(
'date' => '2016-02-28 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
TODAY MINUS 18
DateTime::__set_state(array(
'date' => '1998-03-01 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
TOMORROW MINUS 18
DateTime::__set_state(array(
'date' => '1980-03-01 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
YESTERDAY MINUS 18
DateTime::__set_state(array(
'date' => '1998-02-28 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
Actual result:
--------------
paul@paul-VirtualBox:~$ php php_date_fun.php
TODAY
DateTime::__set_state(array(
'date' => '2016-02-29 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
TOMORROW
DateTime::__set_state(array(
'date' => '2016-03-01 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
YESTERDAY
DateTime::__set_state(array(
'date' => '2016-02-29 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
TODAY MINUS 18
DateTime::__set_state(array(
'date' => '1998-03-01 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
TOMORROW MINUS 18
DateTime::__set_state(array(
'date' => '1980-03-01 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
YESTERDAY MINUS 18
DateTime::__set_state(array(
'date' => '1962-03-01 15:49:56',
'timezone_type' => 3,
'timezone' => 'America/Los_Angeles',
))
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71693&edit=1