在 PHP 中获取 UTC 时间

发布于 2024-12-23 02:18:48 字数 198 浏览 3 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

鸠魁 2024-12-30 02:18:48

使用 gmdate 将始终返回 GMT 日期。语法与date 相同。

Using gmdate will always return a GMT date. Syntax is same as for date.

滿滿的愛 2024-12-30 02:18:48

一个简单的 gmdate() 就足够了

<?php
print gmdate("Y-m-d\TH:i:s\Z");

A simple gmdate() will suffice

<?php
print gmdate("Y-m-d\TH:i:s\Z");
巴黎夜雨 2024-12-30 02:18:48

正如之前在这里回答的,从 PHP 5.2.0 开始,您可以使用 DateTime 类并指定具有 DateTimeZone 实例的 UTC 时区。

DateTime __construct() 文档 建议在创建时传递“now”作为第一个参数DateTime 实例并指定时区来获取当前时间。

$date_utc = new \DateTime("now", new \DateTimeZone("UTC"));

echo $date_utc->format(\DateTime::RFC850); # Saturday, 18-Apr-15 03:23:46 UTC

As previously answered here, since PHP 5.2.0 you can use the DateTime class and specify the UTC timezone with an instance of DateTimeZone.

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.

$date_utc = new \DateTime("now", new \DateTimeZone("UTC"));

echo $date_utc->format(\DateTime::RFC850); # Saturday, 18-Apr-15 03:23:46 UTC
空袭的梦i 2024-12-30 02:18:48
$time = time();
$check = $time+date("Z",$time);
echo strftime("%B %d, %Y @ %H:%M:%S UTC", $check);
$time = time();
$check = $time+date("Z",$time);
echo strftime("%B %d, %Y @ %H:%M:%S UTC", $check);
栖竹 2024-12-30 02:18:48

获取 UTC 日期

gmdate("Y-m-d H:i:s");

获取 UTC 时间戳

time();

即使您的代码中有 date_default_timezone_set,结果也不会有所不同。

Obtaining UTC date

gmdate("Y-m-d H:i:s");

Obtaining UTC timestamp

time();

The result will not be different even you have date_default_timezone_set on your code.

倾城泪 2024-12-30 02:18:48
date("Y-m-d H:i:s", time() - date("Z"))
date("Y-m-d H:i:s", time() - date("Z"))
全部不再 2024-12-30 02:18:48

除了调用 gmdate 之外,您还可以将此代码放在其余代码之前:

<?php
date_default_timezone_set("UTC");
?>

这将使其余与日期/时间相关的调用使用 GMT/UTC 时区。

Other than calling gmdate you can also put this code before your rest of the code:

<?php
date_default_timezone_set("UTC");
?>

That will make rest of your date/time related calls to use GMT/UTC timezone.

季末如歌 2024-12-30 02:18:48

您想使用 gmdate 方法来获取 GMT 日期。

gmdate("Y-m-d H:i:s");

gmdate 日期格式类似于 date() 方法中的格式。

You'd like to use gmdate method to get GMT date.

gmdate("Y-m-d H:i:s");

gmdate date-formats resemble those in the date() method.

寒冷纷飞旳雪 2024-12-30 02:18:48

您可以使用不带参数的gmmktime函数来获取当前UTC时间戳:

$time = gmmktime();
echo date("Y-m-d H:i:s", $time); 

gmmktime仅在您的服务器时间使用UTC时才有效。例如,我的服务器设置为美国/太平洋地区。上面列出的函数反映了太平洋时间。

You can use gmmktime function without arguments to obtain the current UTC timestamp:

$time = gmmktime();
echo date("Y-m-d H:i:s", $time); 

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.

凉世弥音 2024-12-30 02:18:48

根据本地时间,使用字符串 GMT/UTC +/-0400 或 GMT/UTC +/-1000

您的自定义 format 只是缺少 O 来为您提供与本地的时区偏移量时间。

与格林威治时间 (GMT) 的时差(以小时为单位)示例:+0200

date_default_timezone_set('America/La_Paz');
echo date('Y-m-d H:i:s O');

2018-01-12 12:10:11 -0400

但是,为了最大化可移植性/互操作性,我建议使用 ISO8601 日期格式 c

date_default_timezone_set('America/La_Paz');
echo date('c');

2018-01-12T12:10:11-04:00

date_default_timezone_set('Australia/Brisbane');
echo date('c');

2018-01-13T02:10:11+10:00

您还可以使用 gmdate 时区偏移字符串将始终为 +00:00

date_default_timezone_set('America/La_Paz');
echo gmdate('c');

2018-01-12T16:10:11+00:00

date_default_timezone_set('Australia/Brisbane');
echo gmdate('c');

2018-01-12T16:10:11+00:00

with string GMT/UTC +/-0400 or GMT/UTC +/-1000 based on local timings

Your custom format is just missing O to give you the timezone offsets from local time.

Difference to Greenwich time (GMT) in hours Example: +0200

date_default_timezone_set('America/La_Paz');
echo date('Y-m-d H:i:s O');

2018-01-12 12:10:11 -0400

However, for maximized portability/interoperability, I would recommend using the ISO8601 date format c

date_default_timezone_set('America/La_Paz');
echo date('c');

2018-01-12T12:10:11-04:00

date_default_timezone_set('Australia/Brisbane');
echo date('c');

2018-01-13T02:10:11+10:00

You can use also gmdate and the timezone offset string will always be +00:00

date_default_timezone_set('America/La_Paz');
echo gmdate('c');

2018-01-12T16:10:11+00:00

date_default_timezone_set('Australia/Brisbane');
echo gmdate('c');

2018-01-12T16:10:11+00:00

与风相奔跑 2024-12-30 02:18:48

您可以使用以下方法获取 UTC 时间:

date_default_timezone_set('Asia/Calcutta');

$current_date = date("Y/m/d g:i A");

$ist_date = DateTime::createFromFormat(
                        '"Y/m/d g:i A"',
                        $current_date,
                        new DateTimeZone('Asia/Calcutta')
                    );

$utc_date = clone $ist_date;
$utc_date->setTimeZone(new DateTimeZone('UTC'));

echo 'UTC:  ' . $utc_date->format('Y-m-d g:i A');

You can use following to get UTC time:

date_default_timezone_set('Asia/Calcutta');

$current_date = date("Y/m/d g:i A");

$ist_date = DateTime::createFromFormat(
                        '"Y/m/d g:i A"',
                        $current_date,
                        new DateTimeZone('Asia/Calcutta')
                    );

$utc_date = clone $ist_date;
$utc_date->setTimeZone(new DateTimeZone('UTC'));

echo 'UTC:  ' . $utc_date->format('Y-m-d g:i A');
心是晴朗的。 2024-12-30 02:18:48

您需要实际时间戳(数字)
如果您想返回 UTC 的数字时间戳,那么这就是我的理解。 getTimestamp() 函数非常有用。

$ts = new DateTime('NOW', new DateTimeZone('UTC')); // This could say UTC or GMT
echo $ts->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(英国夏令时),而是根据他们所在的欧洲时区而命名。

顺便说一句,如果您想知道是否您当前的时区处于夏令时,您可以执行以下操作:-

$d = date("I");

这将返回 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.

$ts = new DateTime('NOW', new DateTimeZone('UTC')); // This could say UTC or GMT
echo $ts->getTimestamp();

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:-

$d = date("I");

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

自找没趣 2024-12-30 02:18:48
/**
     * Converts a local Unix timestamp to GMT
     *
     * @param   int Unix timestamp
     * @return  int
     */
    function local_to_gmt($time = '')
    {
        if ($time === '')
        {
            $time = time();
        }

        return mktime(
            gmdate('G', $time),
            gmdate('i', $time),
            gmdate('s', $time),
            gmdate('n', $time),
            gmdate('j', $time),
            gmdate('Y', $time)
        );
    }
/**
     * Converts a local Unix timestamp to GMT
     *
     * @param   int Unix timestamp
     * @return  int
     */
    function local_to_gmt($time = '')
    {
        if ($time === '')
        {
            $time = time();
        }

        return mktime(
            gmdate('G', $time),
            gmdate('i', $time),
            gmdate('s', $time),
            gmdate('n', $time),
            gmdate('j', $time),
            gmdate('Y', $time)
        );
    }
笑忘罢 2024-12-30 02:18:48

下面找到获取当前 UTC(协调世界时)时间的 PHP 代码

<?php
// Prints the day
echo gmdate("l") . "<br>";

// Prints the day, date, month, year, time, AM or PM
echo gmdate("l jS \of F Y h:i:s A");
?>

Below find the PHP code to get current UTC(Coordinated Universal Time) time

<?php
// Prints the day
echo gmdate("l") . "<br>";

// Prints the day, date, month, year, time, AM or PM
echo gmdate("l jS \of F Y h:i:s A");
?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文