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?

7
  • how do you store the timezone of the user? As an integer offset? Jun 9, 2011 at 9:42
  • Yes. sorry forgot to mention that. E.G $gmt_offset = -4;
    – Scott
    Jun 9, 2011 at 9:44
  • Is your server in the GMT time zone?
    – Pekka
    Jun 9, 2011 at 9:48
  • @Pekka the server could be anywhere in the world.
    – Scott
    Jun 9, 2011 at 9:50
  • so you will need the server timezone too? Jun 9, 2011 at 9:51

3 Answers 3

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
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

5
  • But would this work? What if the server was in GMT-2 and the user was in GMT-4.
    – Scott
    Jun 9, 2011 at 9:51
  • This answer was given before i came clear you needed the server's offset too. So it won't work like that. Will edit the answer Jun 9, 2011 at 9:58
  • Where do I feed in the $timestamp value of filetime()?
    – Scott
    Jun 9, 2011 at 10:03
  • That would be something like: gmdate("H:i:s", ($timestamp + ($gmt_offset * 3600))); Jun 9, 2011 at 10:10
  • Thanks XpertEase I came to the same code myself. Just testing now.
    – Scott
    Jun 9, 2011 at 10:16
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

7
  • Here you are assuming the server is in GMT-0?
    – Scott
    Jun 9, 2011 at 9: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 9, 2011 at 10:00
  • @LazyOne: and how do you check the timezone of the server time? Jun 9, 2011 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
    – LazyOne
    Jun 9, 2011 at 10:10
  • Sorry LazyOne but I cannot guarantee that the DateTime class will be available on all servers.
    – Scott
    Jun 9, 2011 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.