What are some best practices for handling timestamps in PHP when working with databases?

When working with timestamps in PHP and databases, it is important to ensure that the timestamps are handled consistently to avoid any discrepancies in time zones or formats. One best practice is to always store timestamps in the database in UTC format to maintain consistency across different time zones. When retrieving timestamps from the database, convert them to the desired time zone for display or manipulation.

// Storing timestamp in UTC format in the database
$timestamp = gmdate('Y-m-d H:i:s');

// Retrieving timestamp from the database and converting to desired time zone
$storedTimestamp = '2022-01-01 12:00:00'; // Example retrieved timestamp
$storedTimestampUTC = new DateTime($storedTimestamp, new DateTimeZone('UTC'));
$storedTimestampUTC->setTimezone(new DateTimeZone('America/New_York'));
$convertedTimestamp = $storedTimestampUTC->format('Y-m-d H:i:s');