What potential pitfalls should be considered when using DateTime in PHP to handle dates for file display?

When using DateTime in PHP to handle dates for file display, one potential pitfall to consider is the timezone setting. If the timezone is not explicitly set, it may default to the server's timezone, which could lead to incorrect date and time displays. To avoid this issue, always set the timezone explicitly when working with DateTime objects.

// Set the timezone to the desired value
date_default_timezone_set('America/New_York');

// Create a DateTime object with the file's last modified timestamp
$lastModified = new DateTime();
$lastModified->setTimestamp(filemtime('example.txt'));

// Display the formatted date and time
echo $lastModified->format('Y-m-d H:i:s');