Bug #62326 [Com]: date_diff() function returns false result

From: Date: Mon, 14 Jan 2019 16:26:09 +0000
Subject: Bug #62326 [Com]: date_diff() function returns false result
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-218947@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62326&edit=1

 ID:                 62326
 Comment by:         esavichev at gmail dot com
 Reported by:        ploeger dot private at googlemail dot com
 Summary:            date_diff() function returns false result
 Status:             Open
 Type:               Bug
 Package:            Date/time related
 Operating System:   Windows Server 2008 R2
 PHP Version:        5.3Git-2012-06-14 (snap)
 Block user comment: N
 Private report:     N

 New Comment:

Looks like the same issue on PHP 7.2.10-0ubuntu0.18.04.1
Code to reproduce:

-------------------------------------------------------
echo date_diff(
    date_create('1921-05-02 00:00:00'),
    date_create('1921-05-03 00:00:00')
)->days . "\n"; // 1 - it's correct

echo date_diff(
    date_create('1921-05-01 00:00:00'),
    date_create('1921-05-02 00:00:00')
)->days . "\n"; // 0 - why zero? expected 1!

echo date_diff(
    date_create('1921-05-01 00:00:00'),
    date_create('2019-01-01 00:00:00')
)->days . "\n"; // 35673

echo date_diff(
    date_create('1921-05-02 00:00:00'),
    date_create('2019-01-01 00:00:00')
)->days . "\n"; // 35673 - the same as previous?
-------------------------------------------------------


Previous Comments:
------------------------------------------------------------------------
[2013-10-30 02:20:15] cmbecker69 at gmx dot de

FWIW: this issue apparently is caused by the daylight saving time shift.

------------------------------------------------------------------------
[2012-06-15 11:17:59] ploeger dot private at googlemail dot com

Test script to show the behavior of the bug:

Test script:
---------------
<?php 

const start_date = '2012-06-01 00:00:00'; 
const end_date = '2012-12-01 00:00:00'; 

$timezones = array('europe/Berlin', 'GMT', 'CET', 'UTC'); 

$possibleTimezones = DateTimeZone::listIdentifiers(); 
$possibleTimezones = array_map('strtolower', $possibleTimezones); 

foreach ( $timezones as $testZone ) 
{ 
    $cur = new DateTimeZone($testZone); 
    $start = date_create(start_date, $cur); 
    $end   = date_create(end_date, $cur); 

    $info[] = join(array( 
        'Soll-TZ   : '.$testZone.PHP_EOL, 
        'Differenz : '.( $start->diff($end)->format('%y Jahre %m Monate %d 
Monatstage ( %a tage total )') ).PHP_EOL, 
        'gelisted  : '.( in_array(strtolower($testZone), $possibleTimezones) ? 
'Ja' : 'Nein' ).PHP_EOL, 
        'Alias     : '.( strcasecmp($testZone, $cur->getName()) == 0 ? 'Nein'
: 
'Ja' ).PHP_EOL, 
        'Ist-TZ    : '.( $cur->getName() ).PHP_EOL, 
        'start OfS : '.( ( $cur->getOffset($start) / 60 / 60 ) ).' 
Stunden'.PHP_EOL, 
        'end OfS   : '.( ( $cur->getOffset($end) / 60 / 60 ) ).' 
Stunden'.PHP_EOL 
    )); 
} 

echo join('--- '.PHP_EOL, $info);

Result:
----------------
Soll-TZ   : europe/Berlin
Differenz : 0 Jahre 5 Monate 30 Monatstage ( 183 tage total )
gelisted  : Ja
Alias     : Nein
Ist-TZ    : Europe/Berlin
start OfS : 2 Stunden
end OfS   : 1 Stunden
--- 
Soll-TZ   : GMT
Differenz : 0 Jahre 6 Monate 0 Monatstage ( 183 tage total )
gelisted  : Nein
Alias     : Ja
Ist-TZ    : UTC
start OfS : 0 Stunden
end OfS   : 0 Stunden
--- 
Soll-TZ   : CET
Differenz : 0 Jahre 5 Monate 30 Monatstage ( 183 tage total )
gelisted  : Nein
Alias     : Ja
Ist-TZ    : Europe/Berlin
start OfS : 2 Stunden
end OfS   : 1 Stunden
--- 
Soll-TZ   : UTC
Differenz : 0 Jahre 6 Monate 0 Monatstage ( 183 tage total )
gelisted  : Ja
Alias     : Nein
Ist-TZ    : UTC
start OfS : 0 Stunden
end OfS   : 0 Stunden

------------------------------------------------------------------------
[2012-06-14 19:55:15] juergen78 at gmail dot com

My Hosters OS:
Linux alfa3025 2.6.32-bpo.5-amd64 #1 SMP Wed Jul 20 09:10:04 UTC 2011 x86_64 

Code:
-----
date_default_timezone_set("Europe/Berlin");
$start_timestamp    = date_create('2012-06-01');
$end_timestamp      = date_create('2012-12-01');
$difference = date_diff($start_timestamp, $end_timestamp);
$difference = $difference->format('%M');
echo $difference;

Output:
05

------------------------------------------------------------------------
[2012-06-14 19:13:58] ploeger dot private at googlemail dot com

I could reproduce the bug on WinXP Pro SP3, Win Server 2008 R2 and Win7 Pro.
Default Timezone: Europe/Berlin

Workaround with correct result:
date_default_timezone_set("UTC");
$start_timestamp    = date_create('2012-06-01');
$end_timestamp      = date_create('2012-12-01');
$difference = date_diff($start_timestamp, $end_timestamp);
var_dump($difference);  //6 months

Could you reproduce the bug with:
date.timezone = "Europe/Berlin"
in php.ini on linux?

------------------------------------------------------------------------
[2012-06-14 19:00:49] rasmus@php.net

That output 06 on Linux

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=62326


--
Edit this bug report at https://bugs.php.net/bug.php?id=62326&edit=1


Thread (10 messages)

« previous php.bugs (#218947) next »