Automatic setting of the user's time zone

date_default_timezone_set() - sets the default timezone used by all date/time functions.

Use the following method to automatically change the time zone value for a site.

How it works:

  1. The user enters the site, an ajax request is sent to the server with the browser's time zones (ajax request is sent once).
  2. The PHP script processes the incoming data and installs it into the session.
  3. Next, the session is used to set the locale.

File index.php

<?php
session_start();

// Set the time zone.
if (empty($_SESSION['timezone'])) {
	date_default_timezone_set('America/New_York');
} else {
	date_default_timezone_set($_SESSION['timezone']);
}
?>

<script src="/jquery.min.js"></script>

<script>
	<?php if (empty($_SESSION['timezone'])): ?>
	var gmtDate = new Date();
	$.post('/timezone.php', {data: -gmtDate.getTimezoneOffset() / 60});
	<?php endif; ?>
</script>
HTML

File timezone.php

<?php
session_start();
$data = (isset($_POST['data'])) ? $_POST['data'] : '3';
switch ($data) {
	case '-11':  $_SESSION['timezone'] = 'Pacific/Apia'; break;
	case '-10':  $_SESSION['timezone'] = 'Pacific/Honolulu'; break;
	case '-9':   $_SESSION['timezone'] = 'America/Anchorage'; break;
	case '-8':   $_SESSION['timezone'] = 'America/Los_Angeles'; break;
	case '-7':   $_SESSION['timezone'] = 'America/Denver'; break;
	case '-6':   $_SESSION['timezone'] = 'America/Chicago'; break;
	case '-5':   $_SESSION['timezone'] = 'America/New_York'; break;
	case '-4':   $_SESSION['timezone'] = 'America/Santiago'; break;
	case '-3':   $_SESSION['timezone'] = 'America/Godthab'; break;
	case '-2':   $_SESSION['timezone'] = 'America/Noronha'; break;
	case '-1':   $_SESSION['timezone'] = 'Atlantic/Azores'; break;
	case '0':    $_SESSION['timezone'] = 'Europe/London'; break;
	case '1':    $_SESSION['timezone'] = 'Europe/Paris'; break;
	case '2':    $_SESSION['timezone'] = 'Europe/Minsk'; break;
	case '3':    $_SESSION['timezone'] = 'Europe/Moscow'; break;
	case '3.3':  $_SESSION['timezone'] = 'Asia/Tehran'; break;
	case '4':    $_SESSION['timezone'] = 'Asia/Tbilisi'; break;
	case '4.3':  $_SESSION['timezone'] = 'Asia/Kabul'; break;
	case '5':    $_SESSION['timezone'] = 'Asia/Yekaterinburg'; break;
	case '5.3':  $_SESSION['timezone'] = 'Asia/Calcutta'; break;
	case '5.45': $_SESSION['timezone'] = 'Asia/Katmandu'; break;
	case '6':    $_SESSION['timezone'] = 'Asia/Novosibirsk'; break;
	case '6.3':  $_SESSION['timezone'] = 'Asia/Rangoon'; break;
	case '7':    $_SESSION['timezone'] = 'Asia/Krasnoyarsk'; break;
	case '8':    $_SESSION['timezone'] = 'Asia/Irkutsk'; break;
	case '9':    $_SESSION['timezone'] = 'Asia/Yakutsk'; break;
	case '9.3':  $_SESSION['timezone'] = 'Australia/Darwin'; break;
	case '10':   $_SESSION['timezone'] = 'Asia/Vladivostok'; break;
	case '11':   $_SESSION['timezone'] = 'Asia/Magadan'; break;
	case '12':   $_SESSION['timezone'] = 'Pacific/Auckland'; break;
	case '13':   $_SESSION['timezone'] = 'Pacific/Tongatapu'; break;
	default:     $_SESSION['timezone'] = 'Europe/Moscow'; break; 
}
PHP
05.10.2016, updated 20.02.2021
125

Comments

to add a comment.

Other publications

Regular Expression Shortcodes
Often on sites, it becomes necessary to insert dynamic information blocks into the texts of pages - banners, phones...
36
0
Checking data with regular expressions
A collection of regular expressions with examples in PHP for validating data from form fields.
66
0
Serialize function, possible problems
The serialize () function returns a string representation of any value (array, object, etc.). Using serialize on an...
47
0
How to find out if a request came via AJAX
To distinguish between direct and AJAX requests, simply check the value of the $ _SERVER global variable
78
0
Writing and reading files in PHP
Examples of saving and reading text data and arrays to files.
179
0
Disable PHP caching
Sometimes on hostings PHP opcache caching is enabled by default. Edits were made in the code, but on the site everything is displayed as before...
67
0