Search code examples
phpdatetimetimezonegmtfiletime

PHP file modified time use GMT offset setting to report correct time


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?


Solution

  • 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