如何在PHP中获得格林威治标准时间? [英] How do I get Greenwich Mean Time in PHP?

查看:391
本文介绍了如何在PHP中获得格林威治标准时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置为EST的服务器,并且数据库中的所有记录都设置为EST.我想知道如何将其设置为GMT.我想为我的用户提供一个时区选项.

I have a server which is set to EST, and all records in the database are set to EST. I would like to know how to set it to GMT. I want to offer a time zone option to my users.

推荐答案

无论服务器位于哪个GMT时区,这都是一种获取任何时区时间和日期的极其简便的方法.这是通过time()gmdate()函数完成的. gmdate()函数通常为我们提供GMT时间,但是通过使用time()函数完成技巧,我们可以获得GMT + N或GMT-N,这意味着我们可以获取任何GMT时区的时间.

No matter in which GMT time zone the server is, here is an extremely easy way to get time and date for any time zone. This is done with the time() and gmdate() functions. The gmdate() function normally gives us GMT time, but by doing a trick with the time() function we can get GMT+N or GMT-N, meaning we can get the time for any GMT time zone.

例如,如果您必须抽出GMT + 5的时间,则可以按照以下步骤进行操作

For example, if you have to get the time for GMT+5, you can do it as follows

<?php 
  $offset=5*60*60; //converting 5 hours to seconds.
  $dateFormat="d-m-Y H:i";
  $timeNdate=gmdate($dateFormat, time()+$offset);
?>

现在,如果您必须获取GMT-5的时间,则只需从time()中减去偏移量即可,而不是添加偏移量,例如在以下示例中,我们获取了GMT-4的时间

Now if you have to get the time for GMT-5, you can just subtract the offset from the time() instead of adding to it, like in the following example where we are getting the time for GMT-4

<?php 
  $offset=4*60*60; //converting 4 hours to seconds.
  $dateFormat="d-m-Y H:i"; //set the date format
  $timeNdate=gmdate($dateFormat, time()-$offset); //get GMT date - 4
?>

这篇关于如何在PHP中获得格林威治标准时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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