64 Views
2018 Jul 21Trainingtechnology
img

If you are reading this article ,it means you spent many times in google to detect client timezone.

First of all you should know that there is no way to detect timezone in PHP directly,reason is obvious , PHP is server side so can not detect anything from client.

So What should we do ? I can provide 3 ways.
1-Use api
     You can api as the simplest way , pass the IP address of client and get back timezone name.

2-Use jquery
     Logic is simple , You should find and detect client timezone offset in jquery and then pass offset via ajax to php and convert to readable form in php :
    <script>
      var timezone_offset_minutes = new Date().getTimezoneOffset();
      timezone_offset_minutes = timezone_offset_minutes == 0 ? 0 : -timezone_offset_minutes;
   </script>
and in PHP :
   $timezone_offset_minutes = 330;  // $_GET['timezone_offset_minutes']
   $timezone_name = timezone_name_from_abbr("", $timezone_offset_minutes*60, false);
   echo $timezone_name;
NOTES
   I cant work with timezone_name_from_abbr command ! it returns false

3-Use Timezone DB (Recommended)
  Best way i use know is fill table with all timzones and countries.
  Steps :
  1- Fill table of timezones and countries
  2- Get Client IP address
  3- Get Client  Country
  4- Search in timezone table to find timezone

php

timezone

client timezone in php

how to get clients timezone