我正在尝试转换从youtube api视频Feed收到的时间戳。
收到的时间戳为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小时。请有人帮助我。
答案 0 :(得分:3)
你有没有尝试过时区“亚洲/加尔各答”或
中的任何一个答案 1 :(得分: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);
}
答案 2 :(得分: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'));
答案 3 :(得分:0)
我遇到了同样的问题,希望将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');
如果有人在将来遇到同样的问题,我想分享一下:)
答案 4 :(得分:0)
我最近发现PHP没有内置函数可以在多个时区之间转换时间/日期,因此我也找不到第三方函数。但是,我的确找到了PEAR类,该类具有对多个时区的内置支持,但是无法在php页面中“包含”自身使用。 PEAR类只能在安装后使用,这在每种情况下都不可行。 为了避免安装麻烦,我用PHP编写了3个函数集,这些函数可以让您–
在两个不同的时区之间进行转换。
/**
*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
*/