如何将 php 日期格式转换为 GMT,反之亦然?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5454779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert php date formats to GMT and vice versa?
提问by 0001
i am new to php. i want to write a function where i need user to input date in any date format including DST,into GMT format and again later back into the original entered format.please any body help me.
我是 php 新手。我想编写一个函数,我需要用户以任何日期格式输入日期,包括 DST,输入 GMT 格式,然后再回到原始输入格式。请任何机构帮助我。
回答by Jacob
Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTimeobject.
尽管 gmdate 功能可用。如果您使用的是 PHP 5.2 或更高版本,请考虑使用DateTime对象。
Here's code to switch to GMT
这是切换到 GMT 的代码
$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));
and back to the default timezone...
并回到默认时区...
$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.
使用 DateTime 对象可以让您创建日期时间,就像过程函数一样,不同之处在于您保留对实例的引用。
e.g.
例如
// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');
// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');
// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));
// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');
// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');
There's a whole lot of functions you can use, that are much more readable that the procedural functions.
您可以使用很多函数,它们比过程函数更具可读性。
Explicit example of converting from a timezone to GMT
从时区转换为 GMT 的显式示例
$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');
$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.
echo '<br/>';
// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');
Using your Asia/Kolkata to America/New_York
使用您的亚洲/加尔各答到美国/纽约
date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00
回答by Mike Lewis
Use the gmdate function to convert to GMT time.
使用gmdate 函数转换为 GMT 时间。
For example
例如
$d = '2011-03-28 12:05:20';
$gmt = gmdate('Y-m-d H:i:s',strtotime($d));
回答by rushil
// Convert local time to gmt
// 将当地时间转换为 gmt
public function convertTime($timezone,$time){
$selectedtime = date("Y-m-d H:i",strtotime($time));
$date = new DateTime($selectedtime, new DateTimeZone($timezone));
$date->setTimezone(new DateTimeZone('GMT'));
$convertedtime = strtotime($date->format('Y-m-d H:i'));
return $convertedtime;
}