在 PHP 中获取 UTC 时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8655515/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:11:15  来源:igfitidea点击:

get UTC time in PHP

phpdatetimeutc

提问by Robin Michael Poothurai

How can I get UTC/GMT +/- time stamp using PHP's date() function? For example, if I try

如何使用 PHP 的 date() 函数获取 UTC/GMT +/- 时间戳?例如,如果我尝试

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.

我会得到Unix时间戳;但我需要根据本地时间获取带有字符串 GMT/UTC+/-0400 或 GMT/UTC+/-1000 的 UTC/GMT 时间戳。

回答by nikc.org

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

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

回答by Shankar Damodaran

A simple gmdate()will suffice

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

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

回答by Jessedc

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

正如之前在此处回答的那样,自 PHP 5.2.0 起,您可以使用DateTime该类并通过DateTimeZone.

The DateTime __construct() documentationsuggests passing "now" as the first parameter when creating a DateTime instance and specifying a timezone to get the current time.

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

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

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

回答by Sudhir Bastakoti

$time = time();
$check = $time+date("Z",$time);
echo strftime("%B %d, %Y @ %H:%M:%S UTC", $check);

回答by Allanrbo

date("Y-m-d H:i:s", time() - date("Z"))

回答by StefansArya

Obtaining UTC date

获取UTC日期

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

Obtaining UTC timestamp

获取UTC时间戳

time();

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

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

回答by anubhava

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

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

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

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

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

回答by RicoRico

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

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

$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.

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

回答by Jeff Puckett

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

带有字符串 GMT/UTC +/-0400或 GMT/UTC +/-1000基于本地时间

Your custom formatis just missing Oto give you the timezone offsets from local time.

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

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

与格林威治时间 (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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

回答by Dalip

You can use following to get UTC time:

您可以使用以下方法获取 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');