2
$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.

john
  • 33,520
  • 12
  • 45
  • 62

5 Answers5

6

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
  • 51,324
  • 12
  • 104
  • 132
  • 1
    I'm rather confused why they declared they "Won't Fix" this obvious bug... Just ran into it myself. – colithium Jul 29 '10 at 19:18
  • 1
    I've added a pretty good substitute function at https://bugs.php.net/bug.php?id=51184 that uses the native function if it works, else uses an alternate method. – fbas Oct 16 '11 at 16:15
  • @fbas the bug tracking system is not the place to post work-around scripts, and here isn't the place to advertise them either. – salathe Oct 16 '11 at 22:06
2

Please upgrade to the latest php. This error only occurs on php 5.3 VC6.

Satbir Kira
  • 792
  • 6
  • 21
  • Late answer review: This question was asked two years ago, so thanks for adding your confirmation that the problem has since been fixed. – Sepster Oct 03 '12 at 14:23
0
$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
  • 2,140
  • 1
  • 25
  • 38
  • 1
    I don't think so. Per: http://nl3.php.net/manual/en/datetime.construct.php DateTime::__construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] ) time String in a format accepted by strtotime(). – john Mar 25 '10 at 20:58
  • i found a nice link about it http://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 Mar 25 '10 at 21:14
  • i tested it myslef and have the same result as you – Grumpy Mar 25 '10 at 21:25
0

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

john
  • 33,520
  • 12
  • 45
  • 62
0

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.

CSchulz
  • 10,882
  • 11
  • 60
  • 114
Grumpy
  • 2,140
  • 1
  • 25
  • 38