What are the best practices for automatically adjusting time format based on user language settings in PHP?

When displaying time to users in a web application, it is important to consider their language settings to ensure a seamless experience. One way to automatically adjust the time format based on user language settings in PHP is to use the `setlocale` function to set the appropriate locale for the user. This will allow PHP to display dates and times in the user's preferred format.

// Set the locale based on user's language settings
setlocale(LC_TIME, 'en_US.utf8'); // Change 'en_US.utf8' to user's preferred locale

// Get the current time and format it based on the user's locale
$currentTime = time();
$formattedTime = strftime('%X', $currentTime);

// Display the formatted time to the user
echo $formattedTime;