在 PHP 中获取 UTC 时间
如何使用 PHP 的 date() 函数获取 UTC/GMT +/- 时间戳?例如,如果我尝试,
date("Y-m-d H:i:s", time());
我将获得 Unix 时间戳;但我需要根据本地时间获取带有字符串 GMT/UTC+/-0400 或 GMT/UTC+/-1000 的 UTC/GMT 时间戳。
How can I get UTC/GMT +/- time stamp using PHP's date() function? For example, if I try
date("Y-m-d H:i:s", time());
I will get Unix time stamp; but I need to get UTC/GMT time stamp with string GMT/UTC+/-0400 or GMT/UTC+/-1000 based on local timings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
使用
gmdate
将始终返回 GMT 日期。语法与date
相同。Using
gmdate
will always return a GMT date. Syntax is same as fordate
.一个简单的 gmdate() 就足够了
A simple gmdate() will suffice
正如之前在这里回答的,从 PHP 5.2.0 开始,您可以使用
DateTime
类并指定具有DateTimeZone
实例的 UTC 时区。DateTime __construct() 文档 建议在创建时传递“now”作为第一个参数DateTime 实例并指定时区来获取当前时间。
As previously answered here, since PHP 5.2.0 you can use the
DateTime
class and specify the UTC timezone with an instance ofDateTimeZone
.The DateTime __construct() documentation suggests passing "now" as the first parameter when creating a DateTime instance and specifying a timezone to get the current time.
获取 UTC 日期
获取 UTC 时间戳
即使您的代码中有
date_default_timezone_set
,结果也不会有所不同。Obtaining UTC date
Obtaining UTC timestamp
The result will not be different even you have
date_default_timezone_set
on your code.除了调用
gmdate
之外,您还可以将此代码放在其余代码之前:这将使其余与日期/时间相关的调用使用 GMT/UTC 时区。
Other than calling
gmdate
you can also put this code before your rest of the code:That will make rest of your date/time related calls to use GMT/UTC timezone.
您想使用 gmdate 方法来获取 GMT 日期。
gmdate 日期格式类似于 date() 方法中的格式。
You'd like to use gmdate method to get GMT date.
gmdate date-formats resemble those in the date() method.
您可以使用不带参数的gmmktime函数来获取当前UTC时间戳:
gmmktime仅在您的服务器时间使用UTC时才有效。例如,我的服务器设置为美国/太平洋地区。上面列出的函数反映了太平洋时间。
You can use gmmktime function without arguments to obtain the current UTC timestamp:
gmmktime will only work if your server time is using UTC. For example, my server is set to US/Pacific. the listed function above echos back Pacific time.
您的自定义 format 只是缺少
O
来为您提供与本地的时区偏移量时间。但是,为了最大化可移植性/互操作性,我建议使用 ISO8601 日期格式
c
您还可以使用
gmdate
时区偏移字符串将始终为+00:00
Your custom format is just missing
O
to give you the timezone offsets from local time.However, for maximized portability/interoperability, I would recommend using the ISO8601 date format
c
You can use also
gmdate
and the timezone offset string will always be+00:00
您可以使用以下方法获取 UTC 时间:
You can use following to get UTC time:
您需要实际时间戳(数字)
如果您想返回 UTC 的数字时间戳,那么这就是我的理解。 getTimestamp() 函数非常有用。
我将尝试为任何对 GMT/BST/UTC/DST/WET/CET/EET 感到困惑的人提供一些信息作为起点,但这是我对我所理解的解释 - 很确定它是准确的就通俗地说来说。
什么是时间戳?
时间戳相当于自 1970 年 1 月 1 日以来的秒数。本质上,英国格林威治时间被采用作为所有国家都同意采用的全球标准/基线的时间点。所以因为它是在英国的一个地方采用的,所以恰好 GMT 与 UTC 相同。
GMT、UTC、DST、BST、WET、CET、EET
啊啊!
好的,所以要理解的重要一点是 GMT 和 GMT 时间。 UTC 是相同的,因为它们都以英国伦敦->格林威治的冬天为原点。本质上,UTC = GMT+00:00。
然后,后来采用 UTC 作为引用这一商定的 1970 年 1 月 1 日规则开始的最佳方式,以避免与英国和类似时区国家的时间混淆。 UTC 是标准。 GMT 是一个或多个国家的时区。
令人困惑的是夏令时 (DST)。
现在,英国称其为 BST(英国夏令时间),这基本上是 GMT+01:00 的缩写。欧洲(例如)也承认夏令时,但当他们提到这一变化时,他们不会将其称为 BST(英国夏令时),而是根据他们所在的欧洲时区而命名。
顺便说一句,如果您想知道是否您当前的时区处于夏令时,您可以执行以下操作:-
这将返回 1 表示“是的,我是”或“0”表示“否,我不是”
共有三个欧洲时区。
西欧时区 (WET) 基本上是英国和其他一些国家,因为它们在与格林尼治标准时间 (GMT) 旋转时与地球对齐。
然后是中欧时间,这是大多数欧洲国家的所在地 (UTC+01:00),然后是复活节欧洲时间 (UTC+02:00)
因此,考虑到这一点,当您考虑 DST(夏令时)时您可以假设每一个承认这一点的国家都会将其当前标准时间更新 1 小时。
例如,英国将成为 GMT+01:00(因为我们喜欢称其为 GMT),而快捷方式是 BST(英国夏令时),
法国将英国的夏令时称为 UTC+01:00 或 WET+01:00。
法国的夏令时称为 CEST(中欧夏令时),即 UTC+02:00 或 CET+1:00(因为 CET 已被称为 UTC+01:00)
并且使用东欧时间 - 您明白了...
我希望这会有所帮助,但显然,您需要进行自己的研究。此链接有一个很好的图形,显示区域和更多信息。 https://en.wikipedia.org/wiki/Central_European_Time
You want the actual timestamp (a number)
If you want to return the numeric timestamp for UTC then this is my understanding of it. The getTimestamp() function is pretty useful.
I am going to try and provide a little bit of information as a starting point for anyone that is confused by GMT/BST/UTC/DST/WET/CET/EET but this is my interpretation of what I understand - pretty sure it's accurate as far as speaking in layman's terms.
What is a timestamp?
A timestamp is a numeric equivalent of the number of seconds since 1st January 1970. Essentially, the point at which the time in Greenwich UK was adopted as the global standard/base-line that all countries agreed to adopt. So because it was adopted at a place in the UK, it just happens that GMT is the same as UTC.
GMT vs UTC vs DST vs BST vs WET vs CET vs EET
Arggh!
Okay, so the important bit to understand is that GMT & UTC are the same because they started in the UK London->Greenwich's winter as the origin. Essentially, UTC = GMT+00:00.
Then, at a later time UTC was adopted as the best way to refer to the start of this agreed 1st January 1970 rule to avoid confusing it with the time for UK and similar time zone countries. UTC is the standard. GMT is the time zone for a country/countries.
The bit that gets confusing is when there is Daylight Saving Time (DST).
Now, Britain calls this BST (British Summer Time) which is basically a shortcut for saying GMT+01:00. Europe (as an example) acknowledge DayLight Saving Time too but when they refer to the change they don't call it BST (British Summer Time) the name it based on the European Timezone they are in.
BTW, if you wanted to know if you're current Time zone is in Daylight Saving Time you can do:-
This returns 1 for "Yes I am" or "0" for "No I am not"
There are three European Timezones.
There is the Western European Timezone (WET) which is basically the UK and some other countries because of their alignment on the globe when it spins matches up with GMT.
Then there is Central European time which is where most European countries sit (UTC+01:00) and then Easter European time is (UTC+02:00)
So with that in mind, when you take into account DST (Daylight Saving Time) you can assume that each one of those countries that acknowledges it will update their current standard time by 1 hour.
So UK for example will become GMT+01:00 (because we like to call it GMT) and the shortcut is BST (British Summer Time)
France would call Britain's Summer Time as UTC+01:00 or WET+01:00.
Daylight Saving Time to France is called CEST (Central European Summer Time) which is UTC+02:00 or CET+1:00 (because CET is already known as UTC+01:00)
And with Eastern European Time - you get the idea...
I hope that helps a little bit but obviously, you need to do your own research. This link has a good graphic showing the zones and more info. https://en.wikipedia.org/wiki/Central_European_Time
下面找到获取当前 UTC(协调世界时)时间的 PHP 代码
Below find the PHP code to get current UTC(Coordinated Universal Time) time