How to get time of all timezones in PHP

How to get time of all timezones in PHP

To get an array of all time zones with their time offset from Greenwich, use the following function:

function get_time_timezones()
{
	$zones_array = array();
	$timestamp = time();
	
	$default_timezone = date_default_timezone_get();
	$timezone_list = timezone_identifiers_list();
	
	foreach ($timezone_list as $zone)
	{
		date_default_timezone_set($zone);
		$zones_array[$zone] = date('P', $timestamp);
	}
	
	date_default_timezone_set($default_timezone);
	
	return $zones_array;
}

What do we have here:
$zones_array — our resulting array of time zones with their time offsets
$timestamp — current time in timestamp format
$default_timezone — the current time zone
$timezone_list — list of all time zones

Then, we loop through the list of time zones and set each as the current one for our site:

foreach ($timezone_list as $zone)
{
	date_default_timezone_set($zone);
	$zones_array[$zone] = date('P', $timestamp);
}

Using the function `date()` with the parameter `'P'`, we determine the time offset from Greenwich.
And finally, we restore the original time zone:

date_default_timezone_set($default_timezone);
Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

Your email address will not be published. Required fields are marked *