php php将日期时间转换为UTC

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

php convert datetime to UTC

phpdatetimetimezoneutc

提问by Jeremey

I am in need of an easy way to convert a date time stamp to UTC (from whatever timezone the server is in) HOPEFULLY without using any libraries.

我需要一种简单的方法将日期时间戳转换为 UTC(从服务器所在的任何时区)希望不使用任何库。

采纳答案by Phill Pafford

Try the getTimezone and setTimezone, see the example

尝试getTimezone 和 setTimezone,请参阅示例

(But this does use a Class)

(但这确实使用了一个类)

UPDATE:

更新:

Without any classes you could try something like this:

没有任何课程,你可以尝试这样的事情:

$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");

NOTE: You might need to set the timezone back to the original as well

注意:您可能还需要将时区设置回原始时区

回答by poke

Use strtotimeto generate a timestamp from the given string (interpreted as local time) and use gmdateto get it as a formatted UTC date back.

使用strtotime从给定的字符串(解释为本地时间)生成时间戳,并使用gmdate将其作为格式化的 UTC 日期返回。

Example

例子

As requested, here's a simple example:

根据要求,这是一个简单的示例:

echo gmdate('d.m.Y H:i', strtotime('2012-06-28 23:55'));

回答by joerx

Using DateTime:

使用日期时间:

$given = new DateTime("2014-12-12 14:18:00");
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 14:18:00 Asia/Bangkok

$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 07:18:00 UTC

回答by Attilio

Do this way:

这样做:

gmdate('Y-m-d H:i:s', $timestamp)

or simply

或者干脆

gmdate('Y-m-d H:i:s')

to get "NOW" in UTC.

以UTC格式获取“NOW”。

Check the reference:

检查参考:

http://www.php.net/manual/en/function.gmdate.php

http://www.php.net/manual/en/function.gmdate.php

回答by Johan

If you have a date in this format YYYY-MM-HH dd:mm:ss, you can actually trick php by adding a UTC at the end of your "datetime string" and use strtotime to convert it.

如果您有 YYYY-MM-HH dd:mm:ss 这种格式的日期,您实际上可以通过在“日期时间字符串”的末尾添加一个 UTC 并使用 strtotime 来转换它来欺骗 php。

date_default_timezone_set('Europe/Stockholm');
print date('Y-m-d H:i:s',strtotime("2009-01-01 12:00"." UTC"))."\n";
print date('Y-m-d H:i:s',strtotime("2009-06-01 12:00"." UTC"))."\n";

This will print this:

这将打印:

2009-01-01 13:00:00
2009-06-01 14:00:00

And as you can see it takes care of the daylight savings time problem as well.

正如您所看到的,它也解决了夏令时问题。

A little strange way to solve it.... :)

一个有点奇怪的解决方法....:)

回答by Frank Hou

Convert local time zone string to UTC string.
e.g. New Zealand Time Zone

将本地时区字符串转换为 UTC 字符串。
例如新西兰时区

$datetime = "2016-02-01 00:00:01";
$given = new DateTime($datetime, new DateTimeZone("Pacific/Auckland"));
$given->setTimezone(new DateTimeZone("UTC"));
$output = $given->format("Y-m-d H:i:s"); 
echo ($output);
  • NZDT: UTC+13:00
    if $datetime = "2016-02-01 00:00:01", $output = "2016-01-31 11:00:01";
    if $datetime = "2016-02-29 23:59:59", $output = "2016-02-29 10:59:59";
  • NZST: UTC+12:00
    if $datetime = "2016-05-01 00:00:01", $output = "2016-04-30 12:00:01";
    if $datetime = "2016-05-31 23:59:59", $output = "2016-05-31 11:59:59";
  • NZDT:UTC+13:00
    如果 $datetime = "2016-02-01 00:00:01", $output = "2016-01-31 11:00:01";
    如果 $datetime = "2016-02-29 23:59:59", $output = "2016-02-29 10:59:59";
  • NZST:UTC+12:00
    如果 $datetime = "2016-05-01 00:00:01", $output = "2016-04-30 12:00:01";
    如果 $datetime = "2016-05-31 23:59:59", $output = "2016-05-31 11:59:59";

https://en.wikipedia.org/wiki/Time_in_New_Zealand

https://en.wikipedia.org/wiki/Time_in_New_Zealand

回答by Christos Pontikis

As strtotimerequires specific input format, DateTime::createFromFormatcould be used (php 5.3+ is required)

由于strtotime需要特定的输入格式,因此可以使用DateTime::createFromFormat需要 php 5.3+

// set timezone to user timezone
date_default_timezone_set($str_user_timezone);

// create date object using any given format
$date = DateTime::createFromFormat($str_user_dateformat, $str_user_datetime);

// convert given datetime to safe format for strtotime
$str_user_datetime = $date->format('Y-m-d H:i:s');

// convert to UTC
$str_UTC_datetime = gmdate($str_server_dateformat, strtotime($str_user_datetime));

// return timezone to server default
date_default_timezone_set($str_server_timezone);

回答by aorcsik

I sometime use this method:

我有时使用这种方法:

// It is not importnat what timezone your system is set to.
// Get the UTC offset in seconds:
$offset = date("Z");

// Then subtract if from your original timestamp:
$utc_time = date("Y-m-d H:i:s", strtotime($original_time." -".$offset." Seconds"));

Works allMOSTof the time.

工程全部MOST的时间。

回答by The Coprolal

If you don't mind using PHP's DateTimeclass, which has been available since PHP 5.2.0, then there are several scenarios that might fit your situation:

如果您不介意使用自 PHP 5.2.0 起就可用的 PHP 的DateTime类,那么有几种方案可能适合您的情况:

  1. If you have a $givenDtDateTime object that you want to convert to UTC then this will convert it to UTC:

    $givenDt->setTimezone(new DateTimeZone('UTC'));
    
  2. If you need the original $givenDtlater, you might alternatively want to clone the given DateTime object before conversion of the cloned object:

    $utcDt = clone $givenDt;
    $utcDt->setTimezone(new DateTimeZone('UTC'));
    
  3. If you only have a datetime string, e.g. $givenStr = '2018-12-17 10:47:12', then you first create a datetime object, and then convert it. Note this assumes that $givenStris in PHP's configured timezone.

    $utcDt = (new DateTime($givenStr))->setTimezone(new DateTimeZone('UTC'));
    
  4. If the given datetime string is in some timezone different from the one in your PHP configuration, then create the datetime object by supplying the correct timezone (see the list of timezones PHP supports). In this example we assume the local timezone in Amsterdam:

    $givenDt = new DateTime($givenStr, new DateTimeZone('Europe/Amsterdam'));
    $givenDt->setTimezone(new DateTimeZone('UTC'));
    
  1. 如果您有一个$givenDtDateTime 对象要转换为 UTC,那么这会将其转换为 UTC:

    $givenDt->setTimezone(new DateTimeZone('UTC'));
    
  2. 如果$givenDt稍后需要原始对象,您可能还想在转换克隆对象之前克隆给定的 DateTime 对象:

    $utcDt = clone $givenDt;
    $utcDt->setTimezone(new DateTimeZone('UTC'));
    
  3. 如果您只有一个日期时间字符串,例如$givenStr = '2018-12-17 10:47:12',那么您首先创建一个日期时间对象,然后对其进行转换。请注意,这假设$givenStr在 PHP 配置的时区中。

    $utcDt = (new DateTime($givenStr))->setTimezone(new DateTimeZone('UTC'));
    
  4. 如果给定的日期时间字符串与 PHP 配置中的时区不同,则通过提供正确的时区来创建日期时间对象(请参阅PHP 支持的时区列表)。在这个例子中,我们假设本地时区在阿姆斯特丹:

    $givenDt = new DateTime($givenStr, new DateTimeZone('Europe/Amsterdam'));
    $givenDt->setTimezone(new DateTimeZone('UTC'));
    

回答by MUY Belgium

With PHP 5 or superior, you may use datetime::formatfunction (see documentation http://us.php.net/manual/en/datetime.format.php)

使用 PHP 5 或更高版本,您可以使用datetime::format函数(请参阅文档http://us.php.net/manual/en/datetime.format.php

 echo strftime( '%e %B %Y' , 
    date_create_from_format('Y-d-m G:i:s', '2012-04-05 11:55:21')->format('U')
    );  // 4 May 2012