1

May I know is there anyway to calculate age from user input date and minus my database date?

the user input format is as per asian format (dd/mm/yyyy) while database date was store under date type and format as Y-m-d

How do I calculate it and return to $age variables?

Here is my code that I use to execute the data.

//Date that user keyin
$userDate = $_POST['dob'];
$dateTime = DateTime::createFromFormat('d/m/Y', $userDate);
$myFormat = $dateTime->format('Y-m-d');
//Query execute
$statement->execute(array('date'=>$myFormat));

Please advise, thank you.

Sorry for my bad english.

Jeff
  • 111
  • 2
  • 3
  • 11

1 Answers1

2

Try this one

$stmt = $this->_db->prepare("SELECT TIMESTAMPDIFF(YEAR, date_col , :user_date) AS age FROM your_table"); 
$stmt->bindParam('user_date', $myFormat); 
$stmt->execute();

See demo

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118