-1

I have a date of birth in the format...

30-01-1983

I am trying to work out how to ensure that at least 16 years have passed since this date. A basic age validation.

It seems to be defeating me but I think I am just overcomplicating it. Does anyone have an example they can point me in the direction of?

fightstarr20
  • 10,259
  • 33
  • 132
  • 247

2 Answers2

3

Here is a simple method;

<?php

   $date2=date("d-m-Y");//today's date

   $date1=new DateTime("30-01-1983");
   $date2=new DateTime($date2);

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

   $myage= $interval->y; 

  if ($myage >= 16){ 
     echo "valid age";} 
  else{ 
     echo "Invalid age";}


  ?>

Hope it helps

BlackPearl
  • 2,189
  • 2
  • 27
  • 42
0

It's pretty easy to do manually, but PHP provides a built-in function that does the heavy lifting for you:

http://www.php.net/manual/en/datetime.diff.php

Jason Baker
  • 2,392
  • 3
  • 21
  • 26