If I understand well want to update the server time (by Timezone), if you are in Brazil and the server is American (US) want the scripts run with the Brazilian time.
There are two paths to take, the first and what I recommend is not to change the server time "directly", I mean can change the Timezone yes, but in all registry functions, as enter data in the database prefer to use UTC (or GMT) and at the time of reading the database data you would adjust for the user Timezone.
Why getTimezoneOffset returns 180?
I will first explain the reason of the 180, when it is in Brazil and we run the command:
new Date().getTimezoneOffset();
He will return 180
, this value must be divided by 60
, that will give the number 3
, Brazil and reference to UTC -3
hours. So the 180 is correct, but this I did in hours, you can use the 180 making the script to be -180 seconds, but it gets a little harder maybe, because you would have to use gmmktime
of PHP combined with gmdate
(functions that work with GMT) or else use DateTime::modify
which may be easier (see example below).
Adjust Timezone with javascript
Basically (whether it’s automatic or not) you can do something like an update via ajax:
<?php session_start(); ?>
<?php if (empty($_SESSION['timezonesec'])): ?>
<script>
(function () {
var oReq = new XMLHttpRequest();
var tz = -(new Date().getTimezoneOffset());
oReq.open("GET", "/update-timezone.php?sec=" + tz, true);
oReq.onreadystatechange = function()
{
if (oReq.readyState === 4) {
console.log(oReq.responseText);
}
};
oReq.send(null);
})();
</script>
<?php endif; ?>
The archive update-timezone.php
would look something like:
<?php
session_start();
if (!empty($_GET['sec'])) {
$_SESSION['timezonesec'] = $_GET['sec'];
}
Then create a "global" file (if you already have one just put UTC at the top) that contains something like:
global.php
<?php
session_start();
date_default_timezone_set('UTC');
if (empty($_SESSION['timezonesec'])) {
//Emite "erro" (exceção se não conseguir configurar a timezone)
throw new Exception('Timezone não pode ser definida');
}
$tzs = $_SESSION['timezonesec'];
$sec = ($tzs < 0 ? '-' : '+') . $tzs;
//Ajusta pra plural ou singular (realmente não sei se isto faz diferença para o php)
$sp = ' second' . ($tzs < -1 || $tzs > 1 ? 's' : "");
define('TIMEZONE_SECONDS', $sec . $sp);
And the other scripts you can use the class Datetime (it can be easier to work than function date
):
<?php
require_once 'global.php';
$time = new DateTime();
$time->modify(TIMEZONE_SECONDS);
echo $date->format('Y-m-d h:i:s');
Only that there is a problem that can disturb, the user’s computer may be with some invalid configuration (even for lack of battery).
From my point of view the best is to always record in UTC (or GMT) and adjust the time when displaying the data, if it is an authenticated user you can give the option of it even choose the Timezone desired (I think many sites do this way), the user selects the country and the setting is automatic (like facebook).
Adjust Timezone using back-end only
Something that would be safer against crashes if you want the automatic detection I found on SOEN would use the API of the site maxmind, download both:
- http://www.maxmind.com/download/geoip/api/php/php-latest.tar.gz
- http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Then create a global.php file like this:
<?php
session_start();
//Pega o IP do usuário (proxies podem alterar isto)
$ip = $_SERVER['REMOTE_ADDR'];
if (empty($_SESSION['timezone:' . $ip])) {
//Le os dados, o GeoLiteCity.dat é o arquivo que você baixou
$gi = geoip_open("GeoLiteCity.dat", GEOIP_STANDARD);
$record = geoip_record_by_addr($gi, $ip);
if(isset($record)) {
//Usa a função da API do maxmind e salva em uma sessão
$_SESSION['timezone:' . $ip] = get_time_zone($record->country_code, ($record->region!='') ? $record->region : 0);
}
}
if (empty($_SESSION['timezone:' . $ip])) {
//Emite "erro" (exceção se não conseguir configurar a timezone)
throw new Exception('Timezone não pode ser definida');
}
date_default_timezone_set($_SESSION['timezone:' . $ip]);
And add and all your files this:
<?php
require_once 'global.php';
What is the server Timezone?
– Sergio
It doesn’t solve your problem (actually it does, but it’s not the answer you’re looking for), but this lib can help you: http://pellepim.bitbucket.org/jstz/.
– Eduardo Silva
@Sergio the server Timezone is irrelevant, it has to adapt to the user, but in case he is using the Brasilia.
– Elaine
@Eduardosilva found it interesting, I’ll wait for Sergio’s opinion..
– Elaine
@Elaine each user has 1 server? or the server is yours and knows your Timezone?
– Sergio
@Sergio no, the user just needs to send his tmz to the server for the server to return with the correct time..
– Elaine
If I understand you want to adjust the server time through something on the front end? That’s it?
– Guilherme Nascimento
@Guilhermenascimento getting something from the front end to adapt the server time. For example, I’m in Brazil but the server is German, so something from the front end should be sent to the server to set the time next to me (front end)
– Elaine