如何将php日期格式转换为GMT,反之亦然? [英] how to convert php date formats to GMT and vice versa?

查看:178
本文介绍了如何将php日期格式转换为GMT,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的php。
i想要写一个函数,我需要用户以任何日期格式(包括DST)输入日期格式,格式为GMT格式,并再次返回到原​​始输入格式。请帮助我。

解决方案

尽管gmdate函数可用。如果您使用的是PHP 5.2或更高版本,请考虑使用 DateTime 对象。



这里是切换到GMT的代码

  $ date = new DateTime ); 
$ date-> setTimezone(new DateTimeZone('GMT'));

并返回默认时区...

  $ date = new DateTime('2011-01-01',new DateTimeZone('GMT')); 
$ date-> setTimezone(new DateTimeZone(date_default_timezone_get()));






使用DateTime对象可以创建一个datetime,就像程序功能一样,除了你保留一个实例的引用。



例如

  //午餐时间参考2011年圣诞节。 
$ date = new DateTime('2011-12-25 13:00:00');

//以任何我们指定的格式打印人们看的日期。
echo $ date-> format('D jS M y');

//将时区更改为GMT。
$ date-> setTimezone(new DateTimeZone('GMT'));

//现在打印GMT时区
//中的日期/时间,而不是使用它创建的默认时区。
echo $ date-> format('Y-m-d H:i:s');

//只是为了显示更多的内容,得到上一个星期日
$ date-> modify('上个星期日');

有很多功能可以使用,这是更可读的程序功能。 / p>




从时区转换为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');
//输出:2011-12-24 13:00:00
//在圣诞节前夕,墨尔本的午夜将在圣诞前夕GMT下午1点。

echo'< br />';

//将其转换回澳大利亚/墨尔本
$ date-> setTimezone($ melbourne);
echo $ date-> format('Y-m-d H:i:s');






使用您的亚洲/加尔各答给美国/纽约州

  date_default_timezone_set('亚洲/加尔各答'); 
$ 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);
//输出:2011-03-28 03:30:00


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.

解决方案

Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.

Here's code to switch to 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.

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

$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

这篇关于如何将php日期格式转换为GMT,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆