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');
Keywords
Related Questions
- How can the configuration of server settings, such as AllowOverride in Apache, impact the execution of PHP code locally versus on official servers?
- How can the strtotime function be effectively used to adjust time in PHP scripts, and what are common mistakes to avoid?
- How does the array_sum() function differ from the sum_array() function in PHP?