Get user timezone string with Javascript / PHP?

2019-01-24 18:13发布

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 how to Get timezone offset with Javascript, but I'm still unclear about how can I translate the timezone offset to a timezone string in php, as shown above?

Or, which other method cas I use in Javascript / PHP for getting the timezone string for each user?

I'm really not sure how to approach this.

标签: php timezone
4条回答
贪生不怕死
2楼-- · 2019-01-24 18:48

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)
    
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-24 18:52

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

查看更多
相关推荐>>
4楼-- · 2019-01-24 19:03
  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.
查看更多
Anthone
5楼-- · 2019-01-24 19:06
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;
}
查看更多
登录 后发表回答