How can the interval between birthdates be calculated within a query in PHP and MySQL?

To calculate the interval between birthdates within a query in PHP and MySQL, you can use the DATEDIFF function provided by MySQL. This function calculates the difference in days between two dates. You can then use this result to calculate the interval in years, months, or any other desired format within your PHP code.

$query = "SELECT DATEDIFF(birthdate2, birthdate1) AS interval_days FROM table_name";
$result = mysqli_query($connection, $query);

if ($result) {
    $row = mysqli_fetch_assoc($result);
    $interval_days = $row['interval_days'];
    
    // Calculate interval in years
    $interval_years = floor($interval_days / 365);
    
    echo "Interval between birthdates in years: " . $interval_years;
} else {
    echo "Error executing query: " . mysqli_error($connection);
}