Hello all.

I've run across an interesting (from my perspective) issue with an app I'm maintaining. I wanted a script to wait 10 seconds and see if a particular file had been modified in that interval, like so:

$start_time = time();
sleep(10);
$mod_time = filemtime('filename.txt');
if ($mod_time > $start_time) {
...some code...
}

'date_default_timezone_get()' tells me that the default timezone is GMT, and 'time()' sends back a timestamp set for GMT. However, 'filemtime()' sends back a timestamp set for my local timezone, which is EST (GMT -5).

In php, I get the same timezone offset for both $start_time and $mod_time: 0. So it would seem that both stamps are being treated as GMT.

I need to be able to do the comparison and I don't want to hard code the timezone, so is there a way to get the timezone from the operating system?

Any insight would be seriously appreciated.

    Have you checked out the [man]date/man command? On that manual page, look under the "Timezone" section for the different parameters you can retrieve in regards to the server's timezone.

    EDIT: Also... how is this file being modified? Through FTP? Don't forget, FTP servers can allow commands like MDTM which allow the FTP client to change a file's modified time. If your FTP client utilized this function, it would of course copy the file's modified time from the user's local computer, timezone and all.

      Firstly, filemtime() should return a Unix timestamp of the file modification time. Unix timestamps are NOT timezone dependent.

      If a file appears to have the wrong modification date on it, it may be because it's being accessed in a way by a stupid application / file server which does something incorrectly and sets the timestamps incorrectly. This is particularly likely if you are using it through an incorrectly configured windows file server etc, e.g. a misconfigured Samba server and a win32 application which don't behave in the expected way.

      Timestamps are NOT time zone dependent! Really!

      Mark

        MarkR wrote:

        Timestamps are NOT time zone dependent! Really!

        Directly dependent? No. Indirectly, yes.

        If the FTP client set a file time of 5:13pm and the client lives in the Central Timezone in the US (GMT-6:00), it would be 6 hours behind GMT (non-dst). When you use filemtime() to get a Unix timestamp, all it does is look at the file's modified date, which is "(watever) at 5:13pm" and converts that time into a Unix timestamp.

        Now, you have a timestamp for 5:13pm on that day. Problem is, that 5:13pm was set by a person in CST, so it was actually 11:13pm for the server.

          You just summed up my problem exactly, Brad.
          I didn't find an answer to the original problem, but I managed to find a workaround that does what I need without mixing timezones:

          $start_time = filemtime('filename.txt');
          sleep(10);
          clearstatcache();
          $mod_time = filemtime('filename.txt');
          if ($mod_time > $start_time) {
          ...some code...
          }

          clearstatcache() is the important piece. Without it, the results of the filemtime() call won't change for the whole execution of the script.

          Appreciate the replies. 🙂

            Write a Reply...