当前日期与表格中插入的生日日期之间的差异

Difference between the current date and the birthday date inserted in a form

在表格中,用户必须输入出生日期。 PHP如果他已经成就了18年应该控制

$birth = $_POST['data_nascita']; 
$str_birth = strtotime ($birth );        

$today = date("m/d/Y");
$str_today = strtotime ($today);                

if($today - $str_today < 567648000) {
    echo'you can't drive a car in Italy because you are underage';exit(); 
}
// 18 years * 31536000 second in 1 year = 567648000

如果我在birth字段输入1998年9月14日(今天是2016年9月13日,所以18年还没有完全用完),控件不起作用;它从 1998 年 9 月 15 日开始工作。

为什么我少了 2 天?

我建议使用 DateTime Class,因为它内置了所有日期和时间功能以及数学。

一个例子:

$birth = new DateTime($_POST['date_of_birth']);
$today = new DateTime('now');

$diff = $birth->diff($today);

if($diff->format('%Y') > 18) {
    echo "Can drive";
} else {
    echo "Can't Drive";
}