php - Difference between the current date and the birthday date inserted in a form -
on form, user must enter date of birth. php should control if has accomplished 18 years.
$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 car in italy because underage';exit(); } // 18 years * 31536000 second in 1 year = 567648000
if put on birth field date of 14 september 1998 (today 13 september 2016, 18 years don't entirely spend yet), control doesn't work; works starting 15 september 1998.
why lost 2 days?
i recommend using datetime class has date , time functionality , maths built it.
an example:
$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"; }
Comments
Post a Comment