If you call the difference method on the DateObject object and the following criteria are met:

  • $date2 parameter is before the date/time represented by the instantiated class (i.e. $this)
  • the gap between the dates spans one or more year boundaries
  • the $absolute flag is set to FALSE (i.e. we want a negative value out in the current case)

then you end up with an incorrect result. Essentially, the algorithms for adding or subtracting the whole or partial years in between the two dates appear not to take account of the $year_diff < 0 case.

This seems to be the case for week, month and day granularities.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tomdearden’s picture

I've made a patch that I think addresses the above issue.

dusov’s picture

Patch works. Thank you.

jamix’s picture

Status: Active » Needs review

Works over here, too. Changing status to launch the test bot.

BWPanda’s picture

Status: Needs review » Reviewed & tested by the community

I was having the same problem:

// $membership_expiry['value'] = '2015-04-30 00:00:00'
$now = new DateObject();
$expiry_date = new DateObject($membership_expiry['value'], $membership_expiry['timezone']);

$difference = $expiry_date->difference($now, 'days', FALSE);
return $difference;

The above code was incorrectly returning a positive value. After applying the patch it correctly returns a negative value. RTBC.

cafuego’s picture

Status: Reviewed & tested by the community » Fixed

Applied to 7.x-2.x.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.