tags:

views:

117

answers:

4
$now = new DateTime('now');
$tomorrow = new DateTime('tomorrow');
$next_year = new DateTime('+1 year');

echo "<pre>";
print_r($now->diff($tomorrow));
print_r($now->diff($next_year));
echo "</pre>";

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 10
    [i] => 17
    [s] => 14
    [invert] => 0
    [days] => 6015
)

DateInterval Object
(
    [y] => 1
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 6015
)

any ideas why 'days' shows 6015? why won't it show the total number of days? 1 year difference means nothing to me, since months have varying number of days.

A: 
$now = new DateTime('now');

should be

$now = new DateTime(''2010-01-01 00:00:00'');

more in the manual http://nl3.php.net/manual/en/datetime.diff.php

Grumpy
I don't think so. Per: http://nl3.php.net/manual/en/datetime.construct.phpDateTime::__construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] ) time String in a format accepted by strtotime().
john
i found a nice link about ithttp://ditio.net/2008/06/03/php-datetime-and-datetimezone-tutorial/he does this in a sample $dateTime = new DateTime("now", new DateTimeZone('Europe/Warsaw'));hope it helps
Grumpy
i tested it myslef and have the same result as you
Grumpy
A: 

Ok, looks like http://bugs.php.net/bug.php?id=49778 is the issue here.

john
A: 

its a bug http://bugs.php.net/bug.php?id=49778

Thank you for your bug report.

Days is indeed not set when creating a DateInterval using the constructor. A complication with this is that it is impossible to determine the number of days when months or years are specified, since these vary in length. It is possible to fill in the days field in some cases and leave it 0 in others. In any case, it should be documented that the days field is not always available.

Grumpy
+2  A: 

A more appropriate bug report to follow would be #51184 which focuses on the problem of Windows reporting 6015 days (non-Windows appears OK).

No feedback has been given as yet with regards to whether the fix for #49778 (which deals with a different issue) affects this or if the problem persists. If anyone here could take a look and provide some feedback, that would be very kind of you.

salathe
I'm rather confused why they declared they "Won't Fix" this obvious bug... Just ran into it myself.
colithium