Problem using timezone

Hello

My server work using timezone universal (UTC). How can I get the local timezone that my user is working? Yii2 has a default to get local timezone?

What do you need that for?

If you are mixing timezones on the server you will be in trouble :)

If you are interested in formattting time and date according to the users timezone then you will be much better off using javascript. There are various timeago, etc libs out there.

PHP runs on the server and does not know about the client, nor does it care.

EDIT:

Perhaps something like this: http://timeago.yarp.com/

You need to work on the client - that is use a client side programming language (like javascript).

What you need to do, though, is have your timestamps be in full ISO format, including timezone designators. You can read more at the timeago site.

Hello jacmoe

I need to get user timezone, but I cant use javascript becouse the php code is webservice and a mobile device make the requisition. Maybe can I get the ip of requisiton and set a timezone?

You are right: get the IP, get client location from the IP, find the timezone of the location.

A quick Google brought me this: http://dmitrygusev.blogspot.dk/2010/09/how-to-determine-client-timezone-in-web.html

I hope that helps. I am not versed in webservices :)

You can send device timezone as request parameter.

Thanks Guys.

I will try get by IP.

But if I need configure default timezone in Yii2 like "America/New_York" How can I do it in Yii2?

I believe the only reliable way to get the user’s timezone, is via a postback using JavaScript. You might have a .post or .ajax call from your homepage, and set it in a session variable. Using the IP address would require a lookup to an IP database, and might not be as reliable.

The call in javascript is date().getTimezoneOffset() (http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp)

As for setting your timezone, there may be better ways, but I’ve put it in your site’s index.php with date_default_timezone_set(“America/Chicago”); (You might put the timezone in your config params file.

He can’t use javascript Thomsol. :)

If he could, then there are tons of great libs.

My bad… Thanks!

If the API for the web service call has not been syndicated, I would add a timezone offset to the call. Using a lookup based on IP address is going to be problematic.

Jim

Guys the solution:




$time_zone = getTimeZoneFromIpAddress();

echo 'Your Time Zone is '.$time_zone;


$date = new DateTime("now", new DateTimeZone($time_zone) );

echo "<br>with time zone: " . $date->format('Y-m-d H:i:s') . "<br>";


echo "<br>without time zone: " . date("Y-m-d H:i:s") . "<br>";




function getTimeZoneFromIpAddress(){

    $clientsIpAddress = get_client_ip();


    $clientInformation = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$clientsIpAddress));


    $clientsLatitude = $clientInformation['geoplugin_latitude'];

    $clientsLongitude = $clientInformation['geoplugin_longitude'];

    $clientsCountryCode = $clientInformation['geoplugin_countryCode'];


    $timeZone = get_nearest_timezone($clientsLatitude, $clientsLongitude, $clientsCountryCode) ;


    return $timeZone;


}




function get_client_ip() {

    $ipaddress = '';

    if (getenv('HTTP_CLIENT_IP'))

        $ipaddress = getenv('HTTP_CLIENT_IP');

    else if(getenv('HTTP_X_FORWARDED_FOR'))

        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');

    else if(getenv('HTTP_X_FORWARDED'))

        $ipaddress = getenv('HTTP_X_FORWARDED');

    else if(getenv('HTTP_FORWARDED_FOR'))

        $ipaddress = getenv('HTTP_FORWARDED_FOR');

    else if(getenv('HTTP_FORWARDED'))

        $ipaddress = getenv('HTTP_FORWARDED');

    else if(getenv('REMOTE_ADDR'))

        $ipaddress = getenv('REMOTE_ADDR');

    else

        $ipaddress = 'UNKNOWN';

    return $ipaddress;

}


function get_nearest_timezone($cur_lat, $cur_long, $country_code = '') {

    $timezone_ids = ($country_code) ? DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code)

        : DateTimeZone::listIdentifiers();


    if($timezone_ids && is_array($timezone_ids) && isset($timezone_ids[0])) {


        $time_zone = '';

        $tz_distance = 0;


        //only one identifier?

        if (count($timezone_ids) == 1) {

            $time_zone = $timezone_ids[0];

        } else {


            foreach($timezone_ids as $timezone_id) {

                $timezone = new DateTimeZone($timezone_id);

                $location = $timezone->getLocation();

                $tz_lat   = $location['latitude'];

                $tz_long  = $location['longitude'];


                $theta    = $cur_long - $tz_long;

                $distance = (sin(deg2rad($cur_lat)) * sin(deg2rad($tz_lat)))

                    + (cos(deg2rad($cur_lat)) * cos(deg2rad($tz_lat)) * cos(deg2rad($theta)));

                $distance = acos($distance);

                $distance = abs(rad2deg($distance));

                // echo '<br />'.$timezone_id.' '.$distance;


                if (!$time_zone || $tz_distance > $distance) {

                    $time_zone   = $timezone_id;

                    $tz_distance = $distance;

                }


            }

        }

        return  $time_zone;

    }

    return 'unknown';

}



[color="#006400"]/* moved to Tips, Snippets */[/color]