-7

i have a question. I'm trying to figure out the answer to this question but i can't seem to wrap my mind around it. Hopefully someone can help me. The question is:

Write a function that takes someone's first name, last name, birth year, month and day as arguments and gives back the full name + age. Make sure that the app/programm also gives back the correct results in 2015 or later. Check if the first 2 arguments are strings and the thirds is an integer.

Since I am new to PHP and have no clue about dates (i googled but i can't get my mind wrapped around it) Thanks in advance! :) (wrote some code myself to try but it's useless so i left it out)

Lkopo
  • 4,798
  • 8
  • 35
  • 60
Xereoth
  • 31
  • 6
  • seperation of concerns, make one function to calc the age, and leave the frist/last name out of it. - http://stackoverflow.com/questions/3776682/php-calculate-age – Rufinus Sep 10 '14 at 11:15
  • 2
    Please at least try to write some code before asking for help on SO. – Mark Reed Sep 10 '14 at 11:15
  • I did write some code but it doesn't make any sense at all so i figured better not post it to not cause confusion. – Xereoth Sep 10 '14 at 11:16
  • Can you give example `third argument is an integer` – Sadikhasan Sep 10 '14 at 11:20
  • What you mean to say regarding "also gives back the correct results in 2015 or later. " – Pragnesh Karia Sep 10 '14 at 11:28
  • Hmm, why do your homework when you can just post it on StackOverflow... much easier. – Sverri M. Olsen Sep 10 '14 at 11:29
  • As i posted down below, "lol, it's not my homework its an excercise to learn how to use dates etc. but i don't understand anything of it so i rather ask help and then fiddle with it to figure out how it works :) But thanks anyway " – Xereoth Sep 10 '14 at 11:53

5 Answers5

1
<?php
function getAge($f_name, $l_name, $b_day, $b_month, $b_year) {

  //date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = $b_month . "/" . $b_day . "/" . $b_year;
  //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]));
  return $f_name ." ". $l_name ." is: " . $age;
}
?>
James Lalor
  • 1,236
  • 9
  • 22
  • Thansk for your answer, how ever i guess it works from what i try. but i think it's too complicated for what i actually need :) – Xereoth Sep 10 '14 at 11:25
  • Just done it quickly, obviously there are better ways and you'd also want to check variables to make sure they're an int rather than a string etc etc – James Lalor Sep 10 '14 at 11:29
0

Here is a simple function to do that

//birthday YYYY-MM-DD
function userInfo($fname, $lname, $birthday) {
   //Calc age 
   $age = date_diff(date_create($birthday), date_create('today'))->y;  
   return $fname ." ". $lname . " ".$age;
}

now call it

echo userInfo('John', 'Doe', '1986-02-01');
Saqueib
  • 3,484
  • 3
  • 33
  • 56
  • Cool, this seems to do exactly what i needed. How ever i don't understand the code really. Date_create is a PHP build in function and date_diff also i assume? (i'll try to google on it for more info to better understand it! I just really need to learn how to think in code ^_^ – Xereoth Sep 10 '14 at 11:23
0

use this function

<?php
    function get_the_user($fname, $lname,$m ,$d,$y)


      $birthDate =$m.'/'.$d.'/'.$y;
      //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 "Age is:" . $age;
    return $fname.' '.$lname.'is '.$age.' old';
    ?>
john
  • 567
  • 8
  • 23
0

You can use DateTime class for simple operations with datetimes.

function getAge($name, $surname, $b_year, $b_month, $b_date)
{
    $today = new DateTime();
    $birthdate = new DateTime($b_year . '-' . $b_month . '-' . $b_date);
    $diff = $today->diff($birthdate);

    return $name . ' ' . $surname . ' is ' . $diff->format('%y years') . ' old';
}

echo getAge('Mr.', 'Smith', 1990, 12, 12);

Output:

Mr. Smith is 23 years old

Live preview


Check if the first 2 arguments are strings and the thirds is an integer.

I believe this won't be problem for you :).

Useful resources:

Lkopo
  • 4,798
  • 8
  • 35
  • 60
  • 1
    Thanks a lot, like the one above you this works just fine. It gives me a nice example and a start to fiddle around with and adjust some other things :) and try and make the follow up questions myself :) – Xereoth Sep 10 '14 at 11:52
-1

Why not create a Person object and create some methods to handle the logic and formatting. Like:

class Person {

   public $firstName, $lastName, $dob;

   public function __construct($firstName, $lastName, $dob) {
      $this->firstName = $firstName;
      $this->lastName = $lastName;
      $this->dob = new DateTime($dob);
   }

   public function getFullname() {
      return $firstName . ' ' . $lastName;
   }

   public function getAge($onDate=null) {
      if ($onDate === null) $onDate = date('Y-m-d');
      $onDate = new DateTime($onDate);

      $ageOnDate = $this->dob->diff($onDate)->y;
      return $ageOnDate;
   }

}

$dude = new Person('John', 'Doe', '1980-01-15');
echo $dude->getFullName() . "\n";
echo $dude->getAge() . "\n"; // get his age today
echo $dude->getAge('2015-06-01'); // get his age on June 1st, 2015
ODaniel
  • 566
  • 4
  • 13
  • 1
    "Write a function". They have probably not gotten to object oriented programming yet, so if you are going to do his homework then do it as a function. – Sverri M. Olsen Sep 10 '14 at 11:28
  • This is an excellent exercise case to get acquainted with OO. But he didn't ask for it. That's because he probably doesn't know it exists. So let's not tell him. Or let's... I just thought I'd share the way most programmers would approach this problem. – ODaniel Sep 10 '14 at 11:38
  • lol, it's not my homework its an excercise to learn how to use dates etc. but i don't understand anything of it so i rather ask help and then fiddle with it to figure out how it works :) But thanks anyway – Xereoth Sep 10 '14 at 11:40