1

我正在尝试转换从 youtube api 视频提要收到的时间戳。收到的时间戳是2013-01-11T06:45:52.000Z. 我认为这是 GMT 时间戳(如果我错了,请纠正我)。我编写了一个将时间戳转换为 IST 的函数。

function formatTime($ydatetime) {
    $timestamp = explode("T", $ydatetime);
    $date = $timestamp[0];
    $gmtime = substr($timestamp[1], 0, 8);
    $gmtimestamp = $date . " " . $gmtime;
    $datetime = new DateTime($gmtimestamp, new DateTimeZone('GMT'));
    $datetime->setTimezone(new DateTimeZone('IST'));
    return $datetime->format('Y-m-d H:i:s');
}

但它返回时间戳,2013-01-11 08:45:52因为差异仅为 2 小时。GMT 和 IST 之间的实际差异是 5.30 小时。请有人帮助我。

4

5 回答 5

4

您是否尝试过“亚洲/加尔各答”时区或

http://php.net/manual/en/timezones.indian.php中的任何一个

于 2013-01-11T09:36:00.680 回答
1

尝试

function formatTime($ydatetime) {
   date_default_timezone_set('Asia/Kolkata'); //<--This will set the timezone to IST
   $str = strtotime($ydatetime);
   return date('Y-m-d H:i:s', $str);
}
于 2013-01-11T09:37:24.553 回答
1

我面临同样的问题,想将 GMT 转换为 IST。这个对我有用:

$date = new DateTime('2018-04-08T14:30:00.000Z', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d H:i:s');

如果将来有人遇到同样的问题,只是想分享它:)

于 2018-04-09T13:13:48.030 回答
0

尝试

echo date('Y-m-d H:i:s',strtotime('+330 minutes', 0));  

或者

date_default_timezone_set('Asia/Kolkata');
  echo date('Y-m-d H:i:s');  

首先得到时间

$time = new DateTime('now', new DateTimeZone('UTC'));
    // then convert it to IST by
    $time->setTimezone(new DateTimeZone('IST'));
于 2013-01-11T09:38:57.353 回答
0

I recently found that PHP doesn’t have an inbuilt function for conversion of time/date between multiple time zones and neither could I find a third-party function for the purpose. However, I did find the PEAR class which has inbuilt support for multiple time-zones but it cannot be used by “including” itself in the php page. PEAR class can be used only after installation, which may not be feasible in each and every case. To avoid the hassles of installation I have written a set of 3 functions in PHP which wil allows you to –</p>

  1. Convert GMT to local time zone
  2. Convert local time zone to GMT
  3. Convert between two different time zones.

    /**
     *Convert the time in GMT timestamp into  user's local time zone timestamp
     * @param time $gmttime
     * @param string $timezoneRequired
     * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
     * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
     * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
     * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format                      
    */
    
    
    function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
    {
        $system_timezone = date_default_timezone_get();
    
        date_default_timezone_set("GMT");
        $gmt = date("Y-m-d h:i:s A");
    
        $local_timezone = $timezoneRequired;
        date_default_timezone_set($local_timezone);
        $local = date("Y-m-d h:i:s A");
    
        date_default_timezone_set($system_timezone);
        $diff = (strtotime($local) - strtotime($gmt));
    
        $date = new DateTime($gmttime);
        $date->modify("+$diff seconds");
        $timestamp = $date->format("m-d-Y H:i:s");
        return $timestamp;
    }
    
    /**
    *Use: ConvertGMTToLocalTimezone('2009-02-05 11:54:00.000','Asia/Calcutta');
    *Output: 02-05-2009 17:24:00 || IST = GMT+5.5
    */
     /**
     *Convert the time in user's local time zone timestamp into GMT timestamp  
     * @param time $gmttime
     * @param string $timezoneRequired
     * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
     * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
     * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
     * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format Date:06/02/2009
    */
    
    function ConvertLocalTimezoneToGMT($gmttime,$timezoneRequired)
    {
        $system_timezone = date_default_timezone_get();
    
        $local_timezone = $timezoneRequired;
        date_default_timezone_set($local_timezone);
        $local = date("Y-m-d h:i:s A");
    
        date_default_timezone_set("GMT");
        $gmt = date("Y-m-d h:i:s A");
    
        date_default_timezone_set($system_timezone);
        $diff = (strtotime($gmt) - strtotime($local));
    
        $date = new DateTime($gmttime);
        $date->modify("+$diff seconds");
        $timestamp = $date->format("m-d-Y H:i:s");
        return $timestamp;
    }
    
    /**
    *Use: ConvertLocalTimezoneToGMT('2009-02-05 17:24:00.000','Asia/Calcutta');
    *Output: 02-05-2009 11:54:00 ||  GMT = IST-5.5
    */
    
    
    
    
    /**
     *Convert the time in user's local time zone timestamp into another time zone timestamp
     * @param time $gmttime
     * @param string $timezoneRequired
     * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
     * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
     * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
     * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format Date:06/02/2009.
    */
    
    
    function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
    {
        $system_timezone = date_default_timezone_get();
        $local_timezone = $currentTimezone;
        date_default_timezone_set($local_timezone);
        $local = date("Y-m-d h:i:s A");
    
        date_default_timezone_set("GMT");
        $gmt = date("Y-m-d h:i:s A");
    
        $require_timezone = $timezoneRequired;
        date_default_timezone_set($require_timezone);
        $required = date("Y-m-d h:i:s A");
    
        date_default_timezone_set($system_timezone);
    
        $diff1 = (strtotime($gmt) - strtotime($local));
        $diff2 = (strtotime($required) - strtotime($gmt));
    
        $date = new DateTime($time);
        $date->modify("+$diff1 seconds");
        $date->modify("+$diff2 seconds");
        $timestamp = $date->format("m-d-Y H:i:s");
        return $timestamp;
    }
    
    
    
    /**
    *Use: ConvertLocalTimezoneToGMT('2009-02-05 17:24:00.000','Asia/Calcutta','America/Chicago');
    *Output: 02-05-2009 05:54:00 ||  IST = GMT+5.5, CST = GMT-6 || IST - CST = 11.5
    */
    

    find blog here

于 2019-06-11T06:47:45.287 回答