PHP 5.2.17:将本地时间转换为GMT,将GMT转换为本地时间
在我的应用程序中,我只想将本地时间转换为GMT,将GMT转换为本地时间。我有以下两种方法来做到这一点PHP 5.2.17:将本地时间转换为GMT,将GMT转换为本地时间,php,Php,在我的应用程序中,我只想将本地时间转换为GMT,将GMT转换为本地时间。我有以下两种方法来做到这一点 function GmtTimeToLocalTime($date) { $gmt_time = date("Y-m-d H:i:s", $date); date_default_timezone_set("UTC"); $timestamp_in_gmt = strtotime($gmt_time); date_default_timezone_set('Asi
function GmtTimeToLocalTime($date) {
$gmt_time = date("Y-m-d H:i:s", $date);
date_default_timezone_set("UTC");
$timestamp_in_gmt = strtotime($gmt_time);
date_default_timezone_set('Asia/Calcutta');
$local_time = date("Y-m-d H:i:s", $timestamp_in_gmt);
//date_default_timezone_set("UTC");
return $local_time;
}
function LocalTimeToGmtTime($date) {
$local_time = date("Y-m-d H:i:s", $date);
date_default_timezone_set('Asia/Calcutta');
$timestamp_in_localtime = strtotime($local_time);
date_default_timezone_set("UTC");
$gmt_time = date("Y-m-d H:i:s", $timestamp_in_localtime);
//date_default_timezone_set('Asia/Calcutta');
return $gmt_time;
}
但是我得到了所有输入的日期1970-01-01
请告诉我正确的方法。
提前感谢快捷方式应该是查看并使用
DateTime::setTimezone
将时区从GMT修改为本地时间,反之亦然
编辑:当然,您可以使用
DateTime::setTimestamp
或DateTime::createFromFormat
用时间戳填充此DateTime。快捷方式应该是查看并使用DateTime::setTimezone
将时区从GMT修改为本地时间,反之亦然
编辑:当然,您可以使用
DateTime::setTimestamp
或DateTime::createFromFormat
用时间戳填充这个DateTime,如果您尝试使用DateTime对象,您可能会得到更好的结果:
<?php
function GmtTimeToLocalTime($time) {
$date = new DateTime(date('Y-m-d h:i:s',$time),new DateTimezone('UTC'));
$date->setTimezone(new \DateTimezone('Asia/Calcutta'));
return $date->format("Y-m-d H:i:s");
}
如果您尝试使用DateTime对象,可能会得到更好的结果:
<?php
function GmtTimeToLocalTime($time) {
$date = new DateTime(date('Y-m-d h:i:s',$time),new DateTimezone('UTC'));
$date->setTimezone(new \DateTimezone('Asia/Calcutta'));
return $date->format("Y-m-d H:i:s");
}
在下面的函数中传递latlong以获取时区
function getTimezoneGeo($latForGeo, $lngForGeo) {
$json = file_get_contents("http://api.geonames.org/timezoneJSON?lat=".$latForGeo."&lng=".$lngForGeo."&username=demo");
$data = json_decode($json);
$tzone=$data->timezoneId;
return $tzone;
}
在下面的函数中传递latlong以获取时区
function getTimezoneGeo($latForGeo, $lngForGeo) {
$json = file_get_contents("http://api.geonames.org/timezoneJSON?lat=".$latForGeo."&lng=".$lngForGeo."&username=demo");
$data = json_decode($json);
$tzone=$data->timezoneId;
return $tzone;
}
对于LocalTimeToGmtTime()-1969-12-31 12:03:32作为结果对于GmtTimeToLocalTime()-1970-01-01 06:02:49作为结果对于LocalTimeToGmtTime()-1969-12-31 12:03:32作为结果对于GmtTimeToLocalTime()-1970-01-01 06:02:49作为结果$date=new DateTime::createFromFormat('U',$time,new DateTimeZone('UTC');编译错误-需要$symbolHum不需要$,但需要删除“new”,这是一个输入错误对不起,当我将代码放在我的项目文件夹中时,它不工作致命错误:调用未定义的方法DateTime::createFromFormat(),您有哪种版本的php?拼写正确,所以不能是这样。$date=new DateTime::createFromFormat('U',$time,new DateTimeZone('UTC'));编译错误-需要$symbolHum不需要$,但需要删除“new”,这是一个输入错误对不起,当我将代码放在我的项目文件夹中时,它不工作致命错误:调用未定义的方法DateTime::createFromFormat(),您有哪种版本的php?拼写正确,所以不可能是那样。