Hi, I'm a little confused with php's time and date.

The desired output I want from the functions is like this :

  • Fri Aug 29 15:48:23 UTC+0800 2008

However, it seems that PHP doesn't even add GMT to the date string it returns by default..do I have to set it all the time with date_default_timezone_set function since the country will keep changing? (i can't set in ini file)

Example...

echo date("D M j G:i:s O T Y");
//Fri Aug 29 8:38:14 +0000 UTC 2008

date_default_timezone_set('Asia/Singapore');
echo date("D M j G:i:s O T Y");
//Fri Aug 29 16:38:14 +0800 SGT 2008

also, isn't there anyway to let the server return its correct time within its own timezone by default instead of GMT +0000 as default without setting anything in ini file?

    I believe that PHP runs off of the system time if no timezone directive is configured. You would need to use the ini setting date.timezone (http://nz.php.net/manual/en/timezones.php) to setup a default.

    Also, the server doesn't know what time zone the client is in. You would need an IP to country mapper and adjust the timezone for each user accordingly.

    Also, date doesn't take daylight savings into account and you should use strtotime instead.

      you mean i should do this instead to maintain the daylights savings?

      $timestamp = strtotime('now');
      echo date("(D M j G:i:s O T Y)", $timestamp) ;

      what about using time(); instead?

        date() defaults to the current time() value if no 2nd argument is supplied. By default it should output the time per the server's configured timezone (including daylight savings time adjustments). It will, however, as of PHP 5.1.0, throw a notice-level warning if you have not called date_default_timezone_set() (which you could suppress with the "@" operator, I suppose, if it's too much trouble to call that function).

        If the objective is to output a RFC 2822 format timestamp (e.g. for a RSS output), I would just use gmdate('r') and not have to worry about what timezone I'm in.

          NogDog;10884765 wrote:

          If the objective is to output a RFC 2822 format timestamp (e.g. for a RSS output), I would just use gmdate('r') and not have to worry about what timezone I'm in.

          actually, i would need to know the exact time displayed from the server, taking into account its GMT. It's just that normally, in other languages like javascript, i don't even need to set its default timezone to get it.

          that means if the application written in php is to be deployed to other countries, its ini file will need to keep changing which is a hassle. Can't php by default take unix or windows' GMT settings ?

            I'm not sure I'm following you. The timestamp on the server (and the value returned by time()) is GMT; specifically, the number of seconds since 1970-01-01 00:00:00 GMT. The server's timezone setting is only used to control how that date/time is displayed when requested in human-readable terms. If you use PHP's gmdate() function, at any given time any given server anywhere in the world should output the same exact string reflecting the current GMT (UTC) date/time, assuming the server has it clock correctly set.

            If you want it to in fact display as the server's local time, then the date() function should output that without requiring the use of date_default_timezone_set(). It only raises a notice - not an error - in PHP 5.1.0+ if you don't set it, as a way of saying, "Hey, are you sure you want to use the server's local timezone setting here?" You can suppress that notice with the @ operator or via your error_reporting settings if you don't want to worry about it and just use whatever the server thinks of as its local timezone.

            If this does not address your problem, then I guess I need a better explanation of what you need to have happen.

              NogDog;10884774 wrote:

              If you want it to in fact display as the server's local time, then the date() function should output that without requiring the use of date_default_timezone_set(). It only raises a notice - not an error - in PHP 5.1.0+ if you don't set it, as a way of saying, "Hey, are you sure you want to use the server's local timezone setting here?" You can suppress that notice with the @ operator or via your error_reporting settings if you don't want to worry about it and just use whatever the server thinks of as its local timezone.

              If this does not address your problem, then I guess I need a better explanation of what you need to have happen.

              Ok.. i'll illustrate in this section of the code i just ran on my server (im hosting it locally to test):

              server running on PHP 5.2.5, with GMT set to +0800 :

              <html>
              <script type="text/javascript">
                function getDateTime(){
                  var mydatetime = new Date();
              
              alert(mydatetime);
              //outputs Tue Sep 02 2008 13:54:49 GMT+0800
              
                }
              </script>
              <body onload="getDateTime();">
              <?php
              
              echo gmdate("D M j G:i:s O T Y") . "<br />"; 
              
              
              echo date("D M j G:i:s O T Y") . "<br />"; 
              
              
              //with timezone set.
              date_default_timezone_set('Asia/Singapore'); 
              echo date("D M j G:i:s O T Y") . "<br />"; 
              
              
              /*outputs
              
               Tue Sep 2 5:54:49 +0000 GMT 2008
               Tue Sep 2 5:54:49 +0000 UTC 2008
               Tue Sep 2 13:54:49 +0800 SGT 2008
              
              */
              
              ?>
              </body>
              </html>
              

              as you can see, by default, even though my pc has GMT set to +0800, it won't add it until i specifically set my timezone in php. Whereas the javascript doesn't need me to set its GMT to get its settings.

                So it sounds like you are describing an issue with how PHP is (or in this case is not) able to determine the server's timezone setting. As described in the manual for [man]date_default_timezone_get/man:

                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)
                * Reading the value of the date.timezone ini option (if set)
                * Querying the host operating system (if supported and allowed by the OS)

                If none of the above succeed, date_default_timezone_get will return a default timezone of UTC.

                It therefore would appear that none of the applicable settings/variables are present for PHP to acquire or recognize your PCs local timezone, and so is defaulting to UTC, whereas your browser, which has been specifically coded to work with your specific operating system, is able to access that setting. Or perhaps you have a setting in your php.ini file which is overriding the OS's timezone setting? (See the date.timezone setting in your php.ini file.)

                  actually, my date.timezozne in php.ini is set to "" empty string so it's default..i just don't know why it doesn't get my pc's timezone instead.

                  date_default_timezone_get actually returns me "UTC".

                  It's okay, i think i'll just set it through the php's function for the mean time.

                    Just as a note to avoid future confusion (I had trouble understanding the post at first): where you say "GMT" you mean "timezone". GMT is a particular timezone that has been deprecated in favour of UTC. If anyone "sets" it, it's the International Earth Rotation Service.

                    Offhand, I don't see why PHP doesn't get an answer when it asks your operating system (which it does if it can't find a setting in php.ini or a TZ environment variable). There's the possibility that the operating system is returning something that PHP doesn't understand, of course (what do you have it set to?) Unless your OS is using UTC, of course 🙂. What information does phpinfo() provide about the date extension? Both on your PC (which I understand is having the problem) and the server.

                      Weedpacket;10884946 wrote:

                      which it does if it can't find a setting in php.ini or a TZ environment variable

                      Schooled again. 🙂

                        Weedpacket;10884946 wrote:

                        Offhand, I don't see why PHP doesn't get an answer when it asks your operating system (which it does if it can't find a setting in php.ini or a TZ environment variable). There's the possibility that the operating system is returning something that PHP doesn't understand, of course (what do you have it set to?) Unless your OS is using UTC, of course 🙂. What information does phpinfo() provide about the date extension? Both on your PC (which I understand is having the problem) and the server.

                        Well date in phpinfo shows this :

                        date/time support 	enabled
                        "Olson" Timezone Database Version 	2007.9
                        Timezone Database 	internal
                        Default timezone 	UTC
                        
                        Directive	Local Value	Master Value
                        date.default_latitude	31.7667	31.7667
                        date.default_longitude	35.2333	35.2333
                        date.sunrise_zenith	90.583333	90.583333
                        date.sunset_zenith	90.583333	90.583333
                        date.timezone	no value	no value
                          Write a Reply...