2

Given the $interval is a DateInterval object following conditional statement is true whether the anniversary is within 1 year prior to the current date = 0 or the anniversary is within 1 year post the current date = -0.

$interval = $anniversary->diff($current_Date);
if ($interval->format('%r%y') < 0){
    do something
}
  • I can't use abs because they are both 0
  • I can't use <= 0 because they both are
  • I can't use >= 0 because this sets to true on other cases which I don't want
  • I can't use < 0 because 0 isn't and I want 0 to be true
  • I can't use == 0 because both are true
  • I can't use > 0 because 0 isn't and I want 0 to be true

So I want to be able to say something like is this number signed negative but no such thing exists as far as I can tell. Any thoughts?

** Edit **

The solution is to compare the date objects and decide if they are in the appropriate DateInterval:

if ($anniversary < $current_Date && $interval->format('%r%y') == 0){
vascowhite
  • 18,120
  • 9
  • 61
  • 77
codepuppy
  • 1,130
  • 2
  • 15
  • 25

2 Answers2

2

As the returned value of DateTime::diff() is a DateInterval object, the solution is to use the DateInterval::invert property:-

$interval = $anniversary->diff($current_Date);
if ($interval->invert){
    //do something
}

To quote the manual:-

invert

Is 1 if the interval represents a negative time period and 0 otherwise. See DateInterval::format().

Community
  • 1
  • 1
vascowhite
  • 18,120
  • 9
  • 61
  • 77
0

You should do %r%d instead. All you need to know really is if it is positive or negative, right? Then just get the number of days difference and see if the number is less than 0 or greater than 0. After that, you can do a days%365 to get the year difference, or you can do another %y

  • I am using y because the object takes care of leap years. But yes you have set me thinking could compare the dates first then test if they are in the same year. Hang on going to to test that. – codepuppy Dec 10 '12 at 09:08
  • Ok I don't agree with using %d you can still use %y however you nudged me in the right direction thank you. I have updated the question with the answer actually used. – codepuppy Dec 10 '12 at 09:21