Wednesday, March 1, 2017

Detect user timezone with php using Laravel

I am trying to detect the user timezone and to display some content specific the time in that area.

For that purpose I am using jsTimezoneDetect to get the user timezone. After than i submit that timezone to PHP using AJAX and set a cookie with the user timezone.

jQuery(document).ready(function () {
    var timezone = jstz.determine();
    $.post('timezone/set', {_token: $('meta[name="_token"]').attr('content'), timezone: timezone.name() }, function(data){

    });
});

Here i get some strange things:

1) If I get alert(timezone.name()), I see the user timezone, no matter the browser, but in the PHP Controller after I set the Cookie, Chrome does not display me that cookie.

2) It does not work, if I try to get the cookie like that:

Cookie::get('timezone');

3) It works only in FireFox and only like that:

$_COOKIE['timezone'];

And one global question. As a timezone i get Europe/Helsinki, but setting is not set in my Windows Datetime Settings neither in php.ini. Where does that come from ?




via Vince Carter

Advertisement