Bug #71693 [NEW]: Date Interval Subtraction for Leap Day

From: Date: Tue, 01 Mar 2016 00:09:44 +0000
Subject: Bug #71693 [NEW]: Date Interval Subtraction for Leap Day
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199538@lists.php.net to get a copy of this message
From:             snyderpa at gmail dot com
Operating system: Linux
PHP version:      5.5.32
Package:          Date/time related
Bug Type:         Bug
Bug description:Date Interval Subtraction for Leap Day

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 bug report at https://bugs.php.net/bug.php?id=71693&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71693&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71693&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71693&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71693&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71693&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71693&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71693&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71693&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71693&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71693&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71693&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71693&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71693&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71693&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71693&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71693&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71693&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71693&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71693&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71693&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#199538) next »