What are some alternative methods or functions in PHP for handling and formatting timestamps retrieved from a database?

When retrieving timestamps from a database in PHP, you may want to format them in a human-readable way or perform operations like adding or subtracting time. One alternative method is to use the DateTime class in PHP, which provides a wide range of functions for handling and formatting timestamps.

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

// Create a DateTime object
$date = new DateTime($timestamp);

// Format the timestamp
$formatted_date = $date->format('Y-m-d H:i:s');

// Add 1 day to the timestamp
$date->modify('+1 day');

// Subtract 1 hour from the timestamp
$date->modify('-1 hour');

// Output the formatted timestamp
echo $formatted_date;