3

I'm currently reporting file modified time like so:

$this->newScanData[$key]["modified"] = filemtime($path."/".$file);
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"]);

To me I thought there was nothing wrong with that but a user of my code is reporting the time being 4 hours out. The only reason why I can think of this is because the server is in a different timezone to the user. Each user has a variable I can use $gmt_offset that stores the time zone that user is in. $gmt_offset is stored as a basic float offset.

The server could be in any timezone, not necessarily in GMT-0. The server might not be in the same timezone as the user.

How do I get $modifiedtime to have the correct time for the user in his timezone based on $gmt_offset?

Scott
  • 3,967
  • 9
  • 38
  • 56

3 Answers3

2

filemtime() will return a unix timestamp based on the server's clock. Since you have user to gmt offset available, you must convert the unix timestamp to GMT and then into user's timszone as follows:

<?php
    list($temp_hh, $temp_mm) = explode(':', date('P'));
    $gmt_offset_server = $temp_hh + $temp_mm / 60;
    $gmt_offset_user   = -7.0;
    $timestamp         = filemtime(__FILE__);
    echo sprintf('
        Time based on server time.........: %s
        Time converted to GMT.............: %s
        Time converted to user timezone...: %s
        Auto calculated server timezone...: %s
        ',
        date('Y-m-d h:i:s A', $timestamp),
        date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600),
        date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600 + $gmt_offset_user * 3600),
        $gmt_offset_server
    );

    // Output based on server timezone = PKT (+05:00 GMT) and user timezone = PDT (-07:00 GMT)
    // Time based on server time.........: 2011-06-09 03:54:38 PM
    // Time converted to GMT.............: 2011-06-09 10:54:38 AM
    // Time converted to user timezone...: 2011-06-09 03:54:38 AM
    // Auto calculated server timezone...: 5
Salman A
  • 262,204
  • 82
  • 430
  • 521
1

What you need is the strtotime() function. Changed date to gmdate, converting your servers time to GMT

For example if you need the time format like 10:00:00

gmdate("H:i:s", strtotime($gmt_offset . " hours"));

More info here:

http://php.net/manual/en/function.strtotime.php

http://php.net/manual/en/function.gmdate.php

Chris Laarman
  • 1,559
  • 12
  • 25
1
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"] + ($gmt_offset * 3600));

$gmt_offset should be of type float, not int -- some time zones can have fractional difference, like GMT +09:30 for Adelaide

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • Here you are assuming the server is in GMT-0? – Scott Jun 09 '11 at 09:56
  • @Brady: No, here offset will be relative to the server time. You can add additional correction for the server timezone (e.g. `$server_offset`) – LazyOne Jun 09 '11 at 10:00
  • @LazyOne: and how do you check the timezone of the server time? – Chris Laarman Jun 09 '11 at 10:03
  • @XpertEase2: Well, if you know where your server is located/which timezone **you** have set up there, then `$server_offset` can be hardcoded/declared as Constant. If you need to calculate it dynamically -- look at [DateTime::getOffset](http://uk.php.net/manual/en/datetime.getoffset.php) – LazyOne Jun 09 '11 at 10:10
  • Sorry LazyOne but I cannot guarantee that the DateTime class will be available on all servers. – Scott Jun 09 '11 at 10:17
  • @Brady: DateTime is standard PHP class and is available since PHP v5.2.0 (released 02-Nov-2006) which is obsolete now (only 5.3.x is supported). How old these servers are (in terms of PHP installation) and why would someone still use such old version (< 5.2) if 5.2.x (especially 5.3.x) is definitely faster (no doubts at all)? The only reason -- VERY old LEGACY code. – LazyOne Jun 09 '11 at 10:32
  • @brady: check my answer. You can use `date("P")` on PHP 5.1.3 and later. – Salman A Jun 09 '11 at 10:37