How can PHP be used to handle datetime values retrieved from a database for time calculations?

When handling datetime values retrieved from a database for time calculations in PHP, you can use the DateTime class to easily manipulate and perform calculations on the dates and times. By creating DateTime objects from the database values, you can then use methods like add(), sub(), diff(), etc., to perform various time calculations.

// Retrieve datetime value from the database
$datetimeFromDB = "2022-01-01 12:00:00";

// Create a DateTime object from the retrieved value
$dateTime = new DateTime($datetimeFromDB);

// Perform time calculations, for example, adding 1 day
$dateTime->add(new DateInterval('P1D'));

// Get the new date and time after the calculation
$newDateTime = $dateTime->format('Y-m-d H:i:s');

echo $newDateTime;