0

//Html code

<div class="form-group">
<input name="dateOfBirth" type="date" class="form-control" required data-validation-required-message="Date of birth is required">
</div>

// php code

$todaysDateSpecific = date("Y-m-d");
$dateOfBirth = $_POST['dateOfBirth'];
$customerAge = date_diff(date_create($todaysDateSpecific, date_create($dateOfBirth)));

// The customerAge is not calculated // It desplays error message

Warning: date_diff() expects at least 2 parameters, 1 given in C:\xampp\htdocs\Beer sales and //distribution management system\company_customer_management.php on line 94

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
Michael
  • 1
  • 4

2 Answers2

1

Your issue is in

$customerAge = date_diff(date_create($todaysDateSpecific, date_create($dateOfBirth)));

You have date_create, which returns a DateTime object from a parameter. In your case, you are passing both dates as parameters in one date_create.

Try:

$todaysDateSpecific = date_create("2017-03-15");
$dateOfBirth = date_create($_POST['dateOfBirth']);
$customerAge = date_diff($todaysDateSpecific, $dateOfBirth);
Victor Luna
  • 1,746
  • 2
  • 17
  • 36
0

To calculate the age, get the years between today and DOB:

Assuming the POST['dateOfBirth'] is with format "YYYY-mm-dd"

<?php


$today = date("Y-m-d"); 

$dateOfBirth = $_POST['dateOfBirth']; 

// $dateOfBirth = '1987-10-14'; 


$xtoday =strtotime($today);

$xdateOfBirth =strtotime($dateOfBirth);

$diff = abs($xdateOfBirth - $xtoday); 

$years = floor($diff / (365*60*60*24));

echo $years; 

?>
Ken Lee
  • 6,985
  • 3
  • 10
  • 29