-2

I have an field input="date" in html form and i want to compare the selected date with the current date in php but it s not working

$datan=$_POST['birthday'];
date_default_timezone_set('Europe/Bucharest'); 
$currentdate = date("Y-m-d"); 
if(($currentdate-$datan)/365>=18) 
{
 ---do something 
}
else echo " Your age is below 18 !"
Anais
  • 627
  • 1
  • 8
  • 13

1 Answers1

0

Assuming that the value from your form arrives in the yyyy-mm-dd format leverage the functions PHP supplies for dealing with dates.

date_default_timezone_set('Europe/Bucharest'); 
$datan    = new DateTime($_POST['birthday']);
$rightnow = new DateTime();
$interval = $datan->diff($rightnow,true);
if ($interval->format('%y') > 18) { 
    echo 'Your age is over 18'; 
} else {
    echo 'Your age is below 18!';
}
Dave
  • 5,108
  • 16
  • 30
  • 40