Get user timezone string with Javascript / PHP?

后端 未结 4 1374
猫巷女王i
猫巷女王i 2020-12-06 08:07

I\'m trying to get the user\'s timezone as a string on singup. example:

$timezone = \"Asia/Tel_Aviv\";

While researching the issue, I got

相关标签:
4条回答
  • 2020-12-06 08:31
    1. You cannot get the timezone name from an offset. That's because there are many timezones which have the same offset at any given time, so you can't pick one based on an offset. (If you do, this will bite you in the butt later when the timezone goes into or out of DST, changing the offset.
    2. Your best bet is to do geolocation by IP address (google it, lots of material out there) as a best first guess and then give the user an option to choose his timezone himself.
    0 讨论(0)
  • 2020-12-06 08:36
    function tzone(){
        if(isset($_SESSION["tz"])){ $return = $_SESSION["tz"]; } else {
            $ip = $_SERVER["REMOTE_ADDR"];
            $getip = file_get_contents("http://freegeoip.net/json/$ip");
            $getip = json_decode($getip);
            $lat = $getip->latitude; $lng = $getip->longitude; $country = $getip->country_name;
            $getzone = file_get_contents("http://api.geonames.org/timezoneJSON?lat=$lat&lng=$lng&username=demo"); //you can change "demo" to your own username. its free service
            $getzone = json_decode($getzone);
            $zone = $getzone->timezoneId;
            $_SESSION["tz"] = $zone;
            $return = $_SESSION["tz"];
        }
        return $return;
    }
    
    0 讨论(0)
  • 2020-12-06 08:38

    You can't do this in PHP alone.

    You can use Javascript to set the value in a cookie, then use PHP to read the cookie on the next page (re)load.

    Javascript:

    var dateVar = new Date()
    var offset = dateVar.getTimezoneOffset();
    document.cookie = "offset="+offset;
    

    PHP:

    echo $_COOKIE['offset'];
    

    Use this to convert the offset to the friendly timezone name in PHP. Javascript returns the offset in minutes, while this PHP function expects the input to be in seconds - so multiply by 60. The third parameter is a boolean value of whether or not you are in Daylight Savings Time. Read the manual and update the code to fit your needs.

    echo timezone_name_from_abbr("", intval($_COOKIE['offset'])*60, 0);
    

    http://php.net/manual/en/function.timezone-name-from-abbr.php

    0 讨论(0)
  • 2020-12-06 08:51

    You need to follow answers of Nicholas Pickering and deceze♦ partially. Do not use PHP's timezone_name_from_abbr function(Ref).

    Follow these steps to get UTC time of any timezone set in user system:

    1. Javascript (client-side):

      var dateVar = new Date();
      var offset = dateVar.getTimezoneOffset();
      //getTimezoneOffset - returns the timezone difference between UTC and Local Time
      document.cookie = "offset="+offset;
      
    2. Php (server-side):

      public function convert_utc_time($date)
      {
          $time_difference = isset($_COOKIE['offset'])?$_COOKIE['offset']:'';
          if($time_difference != ''){
              $time = strtotime($date);
              $time = $time + ($time_difference*60); //minutes * 60 seconds
              $date = date("Y-m-d H:i:s", $time);
          } //on failure of js, default timezone is set as UTC below
          return $date;
      }
      ..
      ..
      //in my function
      $timezone = 'UTC';
      $date = $this->convert_utc_time($post_date); //$post_date('Y-m-d H:i:s')
      echo strtotime($date. ' '. $timezone)
      
    0 讨论(0)
提交回复
热议问题