From:             helmut at qweb dot at
Operating system: Linux, Windows
PHP version:      Irrelevant
Package:          Date/time related
Bug Type:         Bug
Bug description:Calculation error in DateTime::add() or DateTime::diff()

Description:
------------
The task was to shift a date-interval like [ begin: 2012-05-07, end:
2012-06-01 ] to another begin date like 2012-05-21 - an end date 2012-06-15
is expected (first example). The function shift_datetimeobject() uses
DateTime::diff() and DateTime::add() to calculate the end date - it fails
in the 3rd of 4 attempts.

The second function shift_timestamp() uses timestamp to solve the same
task. it works like expected.

The bug was found on Windows Server 2008/Apache 2.4.2/PHP 5.4.4 and on
Debian Linux squeeze/PHP 5.3.3-7+squeeze9 with Suhosin-Patch/cli and
libapache2-mod-php5 5.3.3-7+squeeze9

Test script:
---------------
function shift_datetimeobject($begin1, $end1, $begin2) {
    
    $dt_begin1 = new DateTime($begin1);
    $dt_end1 =  new DateTime($end1);

    $span = $dt_begin1->diff($dt_end1);

    $dt_return = new DateTime($begin2);
    return array($dt_return->add($span), $span);
}
function shift_timestamp($begin1, $end1, $begin2) {
    
    $dt_begin1 = new DateTime($begin1);
    $st_begin1 = $dt_begin1->getTimestamp();

    $dt_end1 =  new DateTime($end1);
    $st_end1 = $dt_end1->getTimestamp();

    $span = $st_end1 - $st_begin1;

    $dt_begin2 = new DateTime($begin2);
    $st_begin2 = $dt_begin2->getTimestamp();

    $st_end2 = $st_begin2 + $span;
    $dt_end2 = new DateTime();
    return $dt_end2->setTimestamp($st_end2);   
}

$params = array(
    array('2012-05-07', '2012-06-01', '2012-05-21'),
    array('2012-06-11', '2012-07-13', '2012-06-18'),
    array('2012-06-25', '2012-07-27', '2012-07-02'),
    array('2012-07-09', '2012-07-27', '2012-07-16'),
);
foreach ($params as $p) {
    $erg_object = shift_datetimeobject($p[0], $p[1], $p[2]);
    $erg_stamp  =      shift_timestamp($p[0], $p[1], $p[2]);    
    printf("Params: %s, %s, %s\n", $p[0], $p[1], $p[2]);
    printf('$erg_stamp     = %s' . "\n", $erg_stamp->format('Y-m-d'));
    printf('$erg_object[0] = %s' . "\n", $erg_object[0]->format('Y-m-d'));
    printf("span (object)  = %s\n", $erg_object[1]->format('%a days
%H:%I:%S'));
    if ($erg_object[0] == $erg_stamp) {
        echo "OK";
    } else {
        echo "WRONG!";
    }
    echo "\n\n";
}

Expected result:
----------------
Params: 2012-05-07, 2012-06-01, 2012-05-21
Result: 2012-06-15

Params: 2012-06-11, 2012-07-13, 2012-06-18
Result: 2012-07-20

Params: 2012-06-25, 2012-07-27, 2012-07-02
Result: 2012-08-03

Params: 2012-07-09, 2012-07-27, 2012-07-16
Result: 2012-08-03

Actual result:
--------------
Params: 2012-05-07, 2012-06-01, 2012-05-21
$erg_stamp     = 2012-06-15
$erg_object[0] = 2012-06-15
span (object)  = 25 days 00:00:00
OK

Params: 2012-06-11, 2012-07-13, 2012-06-18
$erg_stamp     = 2012-07-20
$erg_object[0] = 2012-07-20
span (object)  = 32 days 00:00:00
OK

Params: 2012-06-25, 2012-07-27, 2012-07-02
$erg_stamp     = 2012-08-03
$erg_object[0] = 2012-08-04
span (object)  = 32 days 00:00:00
WRONG!

Params: 2012-07-09, 2012-07-27, 2012-07-16
$erg_stamp     = 2012-08-03
$erg_object[0] = 2012-08-03
span (object)  = 18 days 00:00:00
OK

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

Reply via email to