I have collected date of births from a registration page, and stored it in a database (MySQL).
Based on the DoB and the current date I have to determine the age category (adult/youth), and I've done this using the code below which is done at registration.
However I would like to calculate the age as at a certain date, for example on the 01 of June each year.
$tdate = date('Y-m-d');
$from = new datetime($child_dob);
$to =new datetime($tdate);
$age = $from->diff($to)->y;
if ($age > 21)
$agecategory = "Adult";
else
$agecategory = "Youth";
Because the year would keep changing I assume it would be irrelevant in performing the calculation, but I am unsure of how to go about the calculation.
Any help would be appreciated.
You can try like this way-
<?php
$future_date = "01 June,2019"; // date string of future
$june = date('Y-m-d',strtotime($future_date)); // make it Y-m-d format from string
$tdate = date('Y-m-d');
$from = new datetime($june);
$to =new datetime($tdate);
$age = $from->diff($to)->y;
if ($age > 21)
$agecategory = "Adult";
else
$agecategory = "Youth";
?>