How can PHP developers ensure the accuracy of timestamps retrieved from databases when performing time calculations?

When retrieving timestamps from databases in PHP, developers should ensure that the timestamps are in a consistent format, such as UTC, to avoid issues with time zone conversions. Additionally, they should use PHP's built-in DateTime class to perform time calculations accurately, taking into account any time zone differences.

// Retrieve timestamp from database
$timestampFromDB = '2022-01-01 12:00:00';

// Create a DateTime object with the retrieved timestamp
$dateTime = new DateTime($timestampFromDB);

// Set the time zone to UTC for consistency
$dateTime->setTimezone(new DateTimeZone('UTC'));

// Perform time calculations accurately
$dateTime->modify('+1 day');

// Output the updated timestamp in UTC
echo $dateTime->format('Y-m-d H:i:s');