1

As of PHP5.3 (I think) it became a requirement for the date.timezone to be set in the php.ini file, or to be set at runtime via the date_default_timezone_set() function. When you don't set this value you get, an error, something along the lines of this:

PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-4.0/DST' instead in FILE on line LINE.

I was wondering what mechanism it uses to guess the timezone, as it seems to be pretty accurate, and I wanted to know for myself so I can present this error in a more friendly way to the user, or allow them to accept this as the correct timezone.

Mark Tomlin
  • 8,593
  • 11
  • 57
  • 72
  • I know this is non obvious, but **I am not** doing this for web development. I'm using PHP in this instance in an application that runs on a client's desktop. – Mark Tomlin Aug 16 '10 at 23:07

2 Answers2

9

Mike's approach only gets you the web server's time zone. I believe you are trying to get the user's time zone. I wrote a function using jQuery and PHP. This is tested, and does work!

On the PHP page where you are want to have the timezone as a variable, have this snippet of code somewhere near the top of the page:

<?php    
    session_start();
    $timezone = $_SESSION['time'];
?>

This will read the session variable "time", which we are now about to create.

On the same page, in the , you need to first of all include jQuery:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

Also in the , below the jQuery, paste this:

<script type="text/javascript">
    $(document).ready(function() {
        if("<?php echo $timezone; ?>".length==0){
            var visitortime = new Date();
            var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;
            $.ajax({
                type: "GET",
                url: "http://domain.com/timezone.php",
                data: 'time='+ visitortimezone,
                success: function(){
                    location.reload();
                }
            });
        }
    });
</script>

You may or may not have noticed, but you need to change the url to your actual domain.

One last thing. You are probably wondering what the heck timezone.php is. Well, it is simply this: (create a new file called timezone.php and point to it with the above url)

<?php
    session_start();
    $_SESSION['time'] = $_GET['time'];
?>

If this works correctly, it will first load the page, execute the JavaScript, and reload the page. You will then be able to read the $timezone variable and use it to your pleasure! It returns the current UTC/GMT time zone offset (GMT -7) or whatever timezone you are in.

Cheers

</Westy92>

Westy92
  • 19,087
  • 4
  • 72
  • 54
  • That's pretty cool. I was not looking for the timezone of the client, but I'm going to use this code in another project of mine. Very nice work. – Mark Tomlin Apr 10 '11 at 05:08
  • This returns the user's current time zone *offset*. If you want to guess the user's current *time zone*, then you have to do something more elaborate like [jsTimeZoneDetect](http://pellepim.bitbucket.org/jstz/). – Matt Johnson-Pint Sep 09 '13 at 13:50
4

I'd probably use the date_default_timezone_get() function:

string date_default_timezone_get  ( void  )

In order of preference, this function returns the default timezone by:

  • Reading the timezone set using the date_default_timezone_set() function (if any)
  • Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
  • Reading the value of the date.timezone ini option (if set)
  • Querying the host operating system (if supported and allowed by the OS)

http://www.php.net/manual/en/function.date-default-timezone-get.php

Mike Caron
  • 14,351
  • 4
  • 49
  • 77