1

Possible Duplicate:
How to calculate the difference between two dates using PHP?

When a user signs up the system has to check that they are old enough to do so, in this example they have to be atleast 8 years old

$minAge = strtotime(date("d")."-".date("m")."-".(date("Y")-8));
$dob = strtotime($day."-".$month."-".$year);

$minAge = 01-03-2004, $dob = 01-02-2011

I basically need to make sure this person was born before 2004 but I want to know whether I have to convert the timestamps to do a comparison or whether there is a more efficient way.

Any help is appreciated, thank you

Community
  • 1
  • 1

3 Answers3

0

With your $dob timestamp, you can subtract that from the current date's timestamp, and then balance those values versus your minAge. ex:

if(($dob-time())=>$minAge) {
   //OVER 8 YEARS
} else {
   //UNDER 8
}

Although, with this method you are comparing minage as a timestamp without days, hours, minutes or seconds, so keep that in mind when using the "time()" value as described in this example.

Ben Ashton
  • 1,385
  • 10
  • 15
0

I think you just need to do something like this:

$dob = strtotime('1-1-2012');
$minAge = (60*60*24*365*8);//number of seconds of 8 years
if(time()-$dob>= $minAge)
{
   //OK

}
else
{
   //NOT OK
}
hungneox
  • 9,333
  • 12
  • 49
  • 66
0

With PHP 5 you can use DateTime class for this kind of things:

function allowed_to_watch_adult_movies($birthday) {
  $min_age = 18;
  $user = new DateTime($birthday);
  $min_birthday = new DateTime('now - '.$min_age.' years');
  $interval = date_diff($min_birthday, $user);
  return $interval->invert == 1;
}
printf('Kid is %sallowed to watch adult movies<br />' , 
       allowed_to_watch_adult_movies('2004-05-18') ? '' : 'not ' );
printf('Grandpa is %sallowed to watch adult movies, but he does not care<br />' , 
       allowed_to_watch_adult_movies('1954-05-12') ? '' : 'not ' );
Nemoden
  • 8,816
  • 6
  • 41
  • 65
  • You might want to change your function to something... else... before this gets flagged. – BoltClock Mar 24 '12 at 17:44
  • Why would anyone flag answer which contains working code? Or does the word "porn" offend someone? – tereško Mar 24 '12 at 17:48
  • Ok, ok... I did not know, we are so sensitive here - sorry. I thought it is funny, not offending. – Nemoden Mar 24 '12 at 17:50
  • Stack Overflow is a professional community, so yes, we are that sensitive with regards to content. Please keep that in mind. – BoltClock Mar 24 '12 at 17:52