How can PHP be used to calculate time differences between two timestamps stored in a MySQL database?

To calculate time differences between two timestamps stored in a MySQL database using PHP, you can retrieve the timestamps from the database, convert them to Unix timestamps using strtotime(), and then calculate the time difference by subtracting one timestamp from the other. Finally, you can format the time difference as needed using date() or other PHP date functions.

// Retrieve timestamps from MySQL database
$timestamp1 = strtotime($row['timestamp1']);
$timestamp2 = strtotime($row['timestamp2']);

// Calculate time difference
$timeDiff = $timestamp2 - $timestamp1;

// Format time difference as needed
$formattedTimeDiff = date("H:i:s", $timeDiff);

echo "Time difference: " . $formattedTimeDiff;