0

I have a code which calculates the age but I need some modification on this one:

<?php
    //date in mm/dd/yyyy format; or it can be in other formats as well
    $birthDate = $tk_image_geboortedag ."-". $tk_image_geboortemaand ."-". $tk_image_geboortejaar;
    //explode the date to get month, day and year
    $birthDate = explode("-", $birthDate);
    //get age from date or birthdate
    $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
        ? ((date("Y") - $birthDate[2]) - 1)
        : (date("Y") - $birthDate[2]));
        echo "Huidige leeftijd: " . $age;
?>

Now it returns: Huidige leeftijd: 19 (whatever it calculates).

What is need is; If the age is below 2 years I need to display the amount of months. So if the birthdate is 09-11-2016 it must show 12 months and if the amount of months is higher then 23 then show the age in years.

Can someone help me with this?

Regards, Robert

n00bly
  • 395
  • 6
  • 21

1 Answers1

2

You want to use the PHP DateTime object. Read this topic from Paulund:

 // $birthDate = $tk_image_geboortedag ."-". $tk_image_geboortemaand ."-". $tk_image_geboortejaar;

 // The birth date as a DateTime Object
 // format typically YYYY/MM/DD
 // timezone field is optional, shown here simply as illustration. 
 $date1 = new DateTime($tk_image_geboortejaar."-". $tk_image_geboortemaand ."-". $tk_image_geboortedag
          , new DateTimeZone('Europe/Amsterdam')); 

 // The date now as a DateTime Object.
 $date2 =  new DateTime(); 

 $difference = $date1->diff($date2);

This will output the difference in the two dates and show you how many years, months and days, return in a DateInterval object.

You can also see what values the DateInterval object holds:

print_r($difference);

/***
   [y] => 3
   [m] => 5
   [d] => 15
   [h] => 0
   [i] => 0
   [s] => 0
   [weekday] => 0
   [weekday_behavior] => 0
   [first_last_day_of] => 0
   [invert] => 0
   [days] => 1264
   [special_type] => 0
   [special_amount] => 0
   [have_weekday_relative] => 0
   [have_special_relative] => 0
***/

So, back to the code:

/*** Output Years ***/
$age = $difference->y." jaar";

if($difference->m < 24){
    /*** Output months ***/
    $age = $difference->m." maanden";
}

echo "Huidige leeftijd: " . $age;

Also please see this very similar question and its excellent answers.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • Why use `explode` and not the `$tk_image_geboortedag ."-". $tk_image_geboortemaand ."-". $tk_image_geboortejaar;` vars? just change the order... – HTMHell Nov 09 '17 at 09:54
  • Thanks for your answer, perfectly explained :) I only changed one thing, I changed the if($difference->m < 24){ to if($difference->y < 2){. So when the number of y is 2 or smaller then show the m. Or did I miss something? – n00bly Nov 09 '17 at 12:05
  • 1
    @n00bly no, they're the same thing. Different ways of checking the same value for the same threshold. Glad to help you `:-)` – Martin Nov 09 '17 at 12:31
  • @n00bly I dont understand your attempted edit: `$difference->m` is the **total** number of months (`18`), *not* the moths upto a year (`6`). – Martin Nov 20 '17 at 13:51