-1

I'm trying to make a program that subtracts the user's inputted birthdate and the date today.

Here is my code:

$bday = $_POST['bday']; //user input 

$today=date("Y-m-d");
$myage= $today - $bday; //i got a warning message here saying "A non-numeric value encountered" 

Any idea how to fix this?

brombeer
  • 8,716
  • 5
  • 21
  • 27

1 Answers1

0

Please try this snippet, Assuming you are requiring days and/or years as a result. I used the date_diff function for this code. Converting the user input into date/time( has the Y-m-d format) also I hard coded the user input for easier visualisation.

Working Snippet:

$input="2013-12-12"; #User input
$date = strtotime($input); #Convert user input to date time
$date1=date_create(date("Y-m-d")); #date today
$date2=date_create(date('Y-m-d', $date)); #inputted date
$diff=date_diff($date1,$date2); #date_diff function 


echo $diff->format("%Y years or %a days");

Output:

8 years or 3123 days

There is possibly a cleaner version of this. But I believe this is the right flow for your requirements.

Nestor Ceniza Jr
  • 976
  • 3
  • 11