What is the difference between UTC and local time in PHP?

When working with dates and times in PHP, it's important to understand the difference between UTC (Coordinated Universal Time) and local time. UTC is a standardized time format that is independent of time zones, while local time refers to the time in a specific time zone. To convert between UTC and local time in PHP, you can use the DateTime class along with the setTimeZone() method to specify the desired time zone.

// Get the current UTC time
$utcTime = new DateTime('now', new DateTimeZone('UTC'));

// Convert UTC time to local time (e.g. 'America/New_York')
$localTimezone = new DateTimeZone('America/New_York');
$utcTime->setTimezone($localTimezone);

// Display the local time
echo $utcTime->format('Y-m-d H:i:s');