What are the common pitfalls to avoid when working with date and time functions in PHP, particularly when dealing with database values?

One common pitfall when working with date and time functions in PHP, especially when dealing with database values, is not properly handling time zones. To avoid issues with time zone conversions, always set the correct time zone before working with date and time functions.

// Set the default time zone to UTC before working with date and time functions
date_default_timezone_set('UTC');

// Retrieve a datetime value from the database
$datetime = $row['datetime'];

// Convert the datetime to a specific time zone before displaying it
$datetime_utc = new DateTime($datetime, new DateTimeZone('UTC'));
$datetime_utc->setTimezone(new DateTimeZone('America/New_York'));
echo $datetime_utc->format('Y-m-d H:i:s');