How can MySQL be used to calculate date differences in PHP?

To calculate date differences in PHP using MySQL, you can use the DATEDIFF function in your MySQL query to calculate the difference between two dates. You can then retrieve this calculated difference in PHP by fetching the result from the MySQL query.

<?php
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Query to calculate date difference
$query = "SELECT DATEDIFF(end_date, start_date) AS date_diff FROM table_name";
$result = $mysqli->query($query);

// Fetch the result
$row = $result->fetch_assoc();
$date_diff = $row['date_diff'];

// Display the date difference
echo "Date difference is: " . $date_diff . " days";

// Close the connection
$mysqli->close();
?>