usamamashkoor's avatar

get and set local timezone in php

Hi i want to get user local timezone and then set that timezone as default in php. For example if i am from Pakistan i will get local timezone Asia/Karachi then i will set that timezone in php like this date_default_timezone_set("Asia/Karachi"); if some other user is from different country like usa or england then we will get their local timezones and set them in php

i want this solution because i want to convert date to unixtimestamp if date is 2016-13-07 and if i use strtotime('2016-13-07 '); then it give me different unix string for different timezones in php

Thanks

0 likes
4 replies
martinbean's avatar

@usamamashkoor Your application’s timezone is stored in your config/app.php file. If you want to override this for users, then you’ll need to do it in a service provider or something.

EventFellows's avatar

We are using a javascript setup to prefill a timezone dropdown with the timezone that is set in the users browser.

<!-- From https://cdnjs.com/libraries/jstimezonedetect -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
        timezone = jstz.determine();
        $('.timezone_detect').html(timezone.name());
        $('select option:contains(' + timezone.name() + ')').prop('selected',true);
        // $('#selectbox').setAttribute('title', 'test_title');
    });
</script>

You can see it in action here: https://www.eventfellows.com -> click on Add Test Event to see the dropdown.

Beware that this uses the user timezone SETTING, not the users current location (though it should be identical in most situations). Another - but seemingly less reliable - apporach would be to use an IP lookup to determine the users timezone.

1 like
usamamashkoor's avatar

@EventFellows how i can access this in PHP because Javascript is client Side and PHP is server side, I want to set the timezone in the Class constructor when the page Loads in php

Thanks Usama

Please or to participate in this conversation.