date_default_timezone_set() is being applied the same for everyone

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



date_default_timezone_set() is being applied the same for everyone



I want to set the timezone to the visitor's timezone.



I am doing it like this way:


$ip = $_REQUEST['REMOTE_ADDR']; // the IP address to query
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
if($query && $query['status'] == 'success')
date_default_timezone_set($query['timezone']);
else
echo 'Unable to get location';



However, when another visitor visits the site he will have the timezone of the previous visitor ...



Why doesn't date_default_timezone clears? Is there's any solution to that?


date_default_timezone



Any help would be appreciated.



Thanks!




2 Answers
2



$_REQUEST is used to get data from GET/POST request. It should be $_SERVER['REMOTE_ADDR']


$_REQUEST


$_SERVER['REMOTE_ADDR']



Code


$ip = $_SERVER['REMOTE_ADDR']; // the IP address to query
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
if($query && $query['status'] == 'success')
date_default_timezone_set($query['timezone']);
echo 'timezone set to '.$query['timezone'];
else
echo 'Unable to get location';



Output


timezone set to Asia/Kolkata



Explanation:



$_REQUEST['REMOTE_ADDR'] have no value, mean $ip is empty. If you query http://ip-api.com/php/ without ip, it'll get request ip by default and shows that request data not ip data. That's why you're getting same timezone for every visitors.


$_REQUEST['REMOTE_ADDR']


$ip


http://ip-api.com/php/



Bonus



You can get visitors timezone without using any API. $_SERVER['COOKIE'] have timezone of visitors.


$_SERVER['COOKIE']


timezone



snippet


parse_str($_SERVER['HTTP_COOKIE'], $cookie);
if(isset($cookie['timezone']))
date_default_timezone_set($cookie['timezone']);

else
echo 'Unable to get location'; //or do something!





Thank you very much for the solution and the bonus too! I didn't know that the HTTP_COOKIE header would contain such value.
– MatrixCow08
Aug 12 at 7:34


HTTP_COOKIE





You're welcome! If you want more information like visitor's user-agent etc... you can print_r($_SERVER); Happy Coding
– Smartpal
Aug 12 at 7:39


print_r($_SERVER);


Happy Coding



Unfortunately, the answer of "Smartpal" is very good but I still have the same problem with it.



So I've heard that date_default_timezone_set() is outdated and old - That's why I've switched to the DateTime() class.


date_default_timezone_set()


DateTime()



and now instead of using date('G') I can do it like that:


date('G')


$ip = $_SERVER['REMOTE_ADDR']; // the IP address to query
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
$tz = $query['timezone'];
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz));
$dt->setTimestamp($timestamp);
echo $dt->format('G');



This worked fine for me.



Source of the answer: https://stackoverflow.com/a/20289096/8524395






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard