FosAvance
FosAvance

Reputation: 2469

date_diff outputs wrong value

$end=date_create("2013-07-30 00:30:33");
$now=date_create();
$x=date_diff($end,$now);
echo $x->format('%a days');

When I use %a it returns 45 days which is correct, when I use %d it returns 15 days. What is problem there?

Upvotes: 0

Views: 862

Answers (2)

Baba
Baba

Reputation: 95101

Note that date_diff($end,$now); returns DateInterval and it has its own format:

FROM PHP DOC

a = Total number of days as a result of a DateTime::diff() or (unknown) otherwise

And

d = Days, numeric

You can not have 45 days in a single month so its basically using %d or %m month %d days

45 days //or 
1 month 15 days

Upvotes: 1

Roby
Roby

Reputation: 78

Number 15 are the days calculated from difference by the months.

For example: (from http://www.php.net/manual/en/dateinterval.format.php)

<?php

$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);

// %a will output the total number of days.
echo $interval->format('%a total days')."\n";

// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');

?>

The above example will output:

31 total days

1 month, 0 days

Upvotes: 3

Related Questions