What are the best practices for handling server time versus local time in PHP applications?

When handling server time versus local time in PHP applications, it is important to ensure consistency and accuracy across different time zones. One common practice is to store all timestamps in UTC format on the server side and then convert them to the user's local time zone when displaying them. This helps avoid confusion and inconsistencies when dealing with time-related data.

// Set default timezone to UTC
date_default_timezone_set('UTC');

// Get current UTC timestamp
$timestamp = time();

// Convert UTC timestamp to user's local time zone
$user_timezone = new DateTimeZone('America/New_York'); // Example: New York time zone
$user_datetime = new DateTime();
$user_datetime->setTimestamp($timestamp);
$user_datetime->setTimezone($user_timezone);

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