0

Possible Duplicate:
How to get aggregate days from PHP's DateTime::diff?

I have something like this:

$daysDiff = intval($currentDate->diff($dueDate)->format('%R%a'));

in the php documentation for DateInterval::format says:

%R -- "+" for positive interval, "-" for negative

%a -- the total count of days i the interval

I having troubles with the result, always return 6015 as days, with the correct sign, + or -. I try with different dates for $currentdate and $dueDate. Can any body tell me why this behavior.

Thanks

Community
  • 1
  • 1
Oriam
  • 641
  • 10
  • 19
  • 3
    Could you please add the values of `$currentDate` and `$dueDate` that you're using? – andrewsi Aug 23 '12 at 17:32
  • $currentDate = new \DateTime() $dueDate is a value that a get from DB, is a valid DateTime object and always, even with different values a get those 6015 days of difference. – Oriam Aug 23 '12 at 17:46

1 Answers1

0

I think you might be using it wrong.

From the manual page on DateTime::diff

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');

$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Try:

$interval = $currentDate->diff($dueDate);
echo $interval->format('%R%a');

Editted to add....

Huh. I've just run your code, and it works. The problem must be with how you're generating the DateTimes:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');

$daysDiff = intval($datetime1->diff($datetime2)->format('%R%a'));
echo $daysDiff

That outputs '2'

andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • please check the returned value here: $datetime1->diff($datetime2) is an object with some properties like: y for year m for moth ... days for total days, even in this object the value for days is always 6015 – Oriam Aug 23 '12 at 17:42
  • @Oriam - I don't know what you mean, sorry – andrewsi Aug 23 '12 at 17:43
  • @Oriam - see my edit. There's something wrong with the dates that you're generating, I think. – andrewsi Aug 23 '12 at 17:51
  • Nothing my friend, is a fact that this function is not working properly, at least with this parameter '%a', i will try to turn arround with the year count, month count etc, and then make a sum to get the total days :( – Oriam Aug 23 '12 at 17:53
  • @Oriam - it works just fine for me. Have you tried cutting and pasting the code I added in the last edit? – andrewsi Aug 23 '12 at 17:56
  • @Oriam see my response made in the [duplicate question](http://stackoverflow.com/questions/2519261/how-to-get-aggregate-days-from-phps-datetimediff) linked above. – salathe Aug 23 '12 at 17:57
  • Do you try this you are suggesting me?: $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $currentDate->diff($dueDate); echo $interval->format('%R%a'); the result is: "+6015" with any dates as datetime1 and datetime2 :( – Oriam Aug 23 '12 at 17:59