Temporarily Changing the File Time PHP uses the GMT offset setting to report the correct time

I am currently reporting file modification time as follows:

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

For me, I thought that there was nothing wrong with that, but the user of my code reports that the time is 4 hours. The only reason I can think about this is because the server is in a different time zone for the user. Each user has a variable that I can use $gmt_offsetthat stores the time zone in which the user is located. $gmt_offsetstored as the base offset of the float.

The server can be in any time zone, not necessarily in GMT-0. The server may not be in the same time zone as the user.

How do I get $modifiedtimefor the right time for the user in his timezone based $gmt_offset?

+3
source share
3 answers

You need a function strtotime(). Gmdate date changed, converts your server time to GMT

For example, if you need a time format, for example, 10:00:00

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

More details here:

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

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

+1
source

filemtime()will return a unix timestamp based on the server clock. Since you have access to the gmt offset, you must convert the unix timestamp to GMT, and then to the custom 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
+2
source
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"] + ($gmt_offset * 3600));

$gmt_offset float, int - , GMT +09: 30 Adelaide

+1

All Articles