php将日期时间转换为UTC
我需要一种简单的方法将日期时间戳转换为UTC(从服务器所在的任何时区),希望不使用任何库。用于从给定字符串(解释为本地时间)生成时间戳,并将其作为格式化的UTC日期返回 例子 根据要求,这里有一个简单的示例:php将日期时间转换为UTC,php,datetime,timezone,utc,Php,Datetime,Timezone,Utc,我需要一种简单的方法将日期时间戳转换为UTC(从服务器所在的任何时区),希望不使用任何库。用于从给定字符串(解释为本地时间)生成时间戳,并将其作为格式化的UTC日期返回 例子 根据要求,这里有一个简单的示例: echo gmdate('d.m.Y H:i', strtotime('2012-06-28 23:55')); 或者,如果您不需要使用字符串,而需要使用时间组件,那么请尝试,请参见示例 (但这确实使用了类) 更新: 如果没有任何课程,您可以尝试以下内容: $the_date = str
echo gmdate('d.m.Y H:i', strtotime('2012-06-28 23:55'));
或者,如果您不需要使用字符串,而需要使用时间组件,那么请尝试,请参见示例
(但这确实使用了类)
更新:
如果没有任何课程,您可以尝试以下内容:
$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 />");
$the_date=strotime(“2010-01-19 00:00:00”);
echo(日期默认时区get()“
”);
echo(日期(“Y-d-mTG:i:sz”,$the_日期)。“
”;
echo(日期、默认时区、设置(“UTC”)。“
”;
echo(日期(“Y-d-mTG:i:sz”,$the_日期)。“
”;
注意:如果日期的格式为YYYY-MM-HH dd:MM:ss,则可能需要将时区设置回原始时区, 实际上,您可以通过在“datetime字符串”的末尾添加一个UTC来欺骗php,并使用strotTime对其进行转换
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";
这将打印以下内容:
2009-01-01 13:00:00
2009-06-01 14:00:00
正如你所看到的,它也解决了夏令时的问题
有点奇怪的解决方法……) 这样做:
gmdate('Y-m-d H:i:s', $timestamp)
或者干脆
gmdate('Y-m-d H:i:s')
在UTC中获取“现在”
检查参考:
试试看
回音日期('F d Y',标准时间('2010-01-19 00:00:00')
将输出:
2010年1月19日
您应该更改格式时间以查看其他输出我有时使用此方法:
// 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"));
大部分时间都可以使用。对于PHP5或更高版本,您可以使用datetime::format函数(参见文档) 由于需要特定的输入格式,因此可以使用(PHP5.3+是必需的) 使用日期时间:
$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
将本地时区字符串转换为UTC字符串。
e、 g.新西兰时区
$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
如果$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”
通用标准化功能,用于格式化从任何时区到其他时区的任何时间戳。 对于在关系数据库中存储来自不同时区的用户的日期时间戳非常有用。对于数据库比较,将时间戳存储为UTC,并与
gmdate('Y-m-d H:i:s')一起使用
用法:
作为改进(我不理解他的“Y-d-mTG:I:sz”,他建议恢复时区)。
因此,我提出了以下建议(我将HMTL格式更改为纯文本…):
或者,您可以尝试以下方法:
<?php echo (new DateTime("now", new DateTimeZone('Asia/Singapore')))->format("Y-m-d H:i:s e"); ?>
这将输出:
2017-10-25 17:13:20亚洲/新加坡
如果只想显示只读日期,可以在文本输入框的值属性中使用此选项
如果您不想显示您所在的地区/国家/地区,请删除“e”。按照以下步骤在用户的本地系统中设置任何时区的UTC时间(web应用程序需要将不同时区保存到UTC):
Javascript(客户端):
Php(服务器端):
如果您不介意使用PHP的类(自PHP5.2.0以来一直可用),那么有几个场景可能适合您的情况:
如果您有一个要转换为UTC的$givent
日期时间对象,则这会将其转换为UTC:
$givenDt->setTimezone(new DateTimeZone('UTC'));
如果以后需要原始的$givent
,也可以在转换克隆对象之前克隆给定的DateTime对象:
$utcDt = clone $givenDt;
$utcDt->setTimezone(new DateTimeZone('UTC'));
如果您只有一个日期时间字符串,例如,$givenStr='2018-12-17 10:47:12'
,则首先创建一个日期时间对象,然后将其转换。注意:这假设$givenStr
位于PHP配置的时区中
$utcDt = (new DateTime($givenStr))->setTimezone(new DateTimeZone('UTC'));
如果给定的datetime字符串所在的时区与PHP配置中的时区不同,则通过提供正确的时区来创建datetime对象(请参阅)。在本例中,我们假设阿姆斯特丹的本地时区为:
$givenDt = new DateTime($givenStr, new DateTimeZone('Europe/Amsterdam'));
$givenDt->setTimezone(new DateTimeZone('UTC'));
您应该只使用gmdate
,而不是将默认时区更改为UTC。因为gmdate
自动使用UTC.-1,因为您不想为此更改全局默认时区。它只是要求产生非预期的副作用。@Phill这里的“sz”是什么?我想它应该是“Y-m-dTG…”而不是“Y-d-mTG…”它在laravel项目中对我不起作用。它的回报是一样的date@cherouvim我不认为这是必要的,但还是添加了一个……希望能有所帮助。gmdate()是我希望很久以前偶然发现的PHP函数之一。nl2br,有人吗?GMT和UTC并不总是一样的。@JaySheth实际上,UTC和GMT是同义词。@poke GMT尊重夏令时,UTC不尊重夏令时,这就是为什么使用它,所以您不必在这两个问题中考虑任何其他因素。首先你不应该减去,你应该加上偏移量。第二,如果本地时区当前在DST上,而所需日期不在DST上,则这不起作用。偏移量将减少一小时。反之亦然。因此,是的,这在大多数情况下都有效,但并不总是有效。我真的不明白为什么OP没有选择你的建议:D
@php\u nub\u qq可能是因为它通过更改整个脚本的时区来工作,这可能不是我们想要的,也可能会产生意外的后果。@Felwise如果你跳过第一行,它不会起作用,这只是为了说明你应该用你的答案来解释这是如何改进Phil的答案的。您在代码中更改了什么,为什么更好?更改默认PHP时区以转换两个时间戳
<?php echo (new DateTime("now", new DateTimeZone('Asia/Singapore')))->format("Y-m-d H:i:s e"); ?>
var dateVar = new Date();
var offset = dateVar.getTimezoneOffset();
//getTimezoneOffset - returns the timezone difference between UTC and Local Time
document.cookie = "offset="+offset;
public function convert_utc_time($date)
{
$time_difference = isset($_COOKIE['offset'])?$_COOKIE['offset']:'';
if($time_difference != ''){
$time = strtotime($date);
$time = $time + ($time_difference*60); //minutes * 60 seconds
$date = date("Y-m-d H:i:s", $time);
} //on failure of js, default timezone is set as UTC below
return $date;
}
..
..
//in my function
$timezone = 'UTC';
$date = $this->convert_utc_time($post_date); //$post_date('Y-m-d H:i:s')
echo strtotime($date. ' '. $timezone)
$givenDt->setTimezone(new DateTimeZone('UTC'));
$utcDt = clone $givenDt;
$utcDt->setTimezone(new DateTimeZone('UTC'));
$utcDt = (new DateTime($givenStr))->setTimezone(new DateTimeZone('UTC'));
$givenDt = new DateTime($givenStr, new DateTimeZone('Europe/Amsterdam'));
$givenDt->setTimezone(new DateTimeZone('UTC'));