0
$mpsAgeT = $today->diff($mpsAgeT);

Where $today is:

object(DateTime)[37]
  public 'date' => string '2019-06-17 13:40:56.888563' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Sofia' (length=12)

and $mpsAgeT is:

object(DateTime)[38]
  public 'date' => string '2016-06-15 13:40:56.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Sofia' (length=12)

Returns me 3 years, but 15.06 is an older date, so I want to return 4 years, 3 are passed and 4 year is started.

If I just added 1 year, it will not work if date is not passed or equal.

For example:

17.06.2019 and 17.06.2016 - should returns me 3
17.06.2019 and 19.06.2016 - should returns me 3
17.06.2019 and 15.06.2016 - should returns me 4
MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
  • Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Dave Jun 17 '19 at 11:20
  • So, add one unless the other properties on `$mpsAgeT` are 0? If `$today` is '2019-06-14' it'll return 2 and you expect 3, right ? – msg Jun 17 '19 at 11:20
  • @msg Yeah, right! – MorganFreeFarm Jun 17 '19 at 11:21

2 Answers2

3

As I suggested in the comments, you always have to add one, unless the dates are exactly equal to get your expected result:

$diff = $today->diff($mpsAgeT);
$years = $diff->y;

 if (addOne($diff)) {
   $years++;
 }

function addOne($interval) {
    // Iterate precision properties of Interval object
    // In this case, month and days, but can add hours, minutes (check edits)
    // If it's anything other than exact years, should add one
    $props = ['m', 'd'];
    foreach ($props as $prop) {
        // This returns as soon as a property is not 0
        if ($interval->{$prop} !== 0)
          return true;
    }
    return false;
}

Demo

Alternate solution without using Intervals:

// Find year difference
$years = $today->format('Y') - $mpsAgeT->format('Y');
// Add to original date (sets it to the same date of the current year)
// Only add if the date has passed
if ($today > $mpsAgeT->modify('+' . $years . ' years')) {
   $years++;
}

Demo

msg
  • 7,863
  • 3
  • 14
  • 33
1

You can simply check if all other variables are not 0

$today = new DateTime('2019-06-17 13:40:56.888563');
$mpsAgeT = new DateTime('2016-06-17 13:40:56.888563');
    $interval = $today->diff($mpsAgeT);
    $diff = $interval->format('%y');
    if (!($interval->d === 0 && $interval->m === 0 && $interval->h === 0 && $interval->i === 0 && $interval->s === 0)) {
        $diff++;
    }
    echo $diff; // returns 3

$today = new DateTime('2019-06-17 13:40:56.888563');
$mpsAgeT = new DateTime('2016-06-19 13:40:56.888563');
$interval = $today->diff($mpsAgeT);
$diff = $interval->format('%y');
if (!($interval->d === 0 && $interval->m === 0 && $interval->h === 0 && $interval->i === 0 && $interval->s === 0)) {
    $diff++;
}
echo $diff;// returns 3

$today = new DateTime('2019-06-17 13:40:56.888563');
$mpsAgeT = new DateTime('2016-06-15 13:40:56.888563');
$interval = $today->diff($mpsAgeT);
$diff = $interval->format('%y');
if (!($interval->d === 0 && $interval->m === 0 && $interval->h === 0 && $interval->i === 0 && $interval->s === 0)) {
    $diff++;
}
echo $diff;// returns 4
angel.bonev
  • 2,154
  • 3
  • 20
  • 30