简体   繁体   English

在PHP中:如何以字符串形式以UTC格式存储当前时间/日期?

[英]In PHP: How to store the current time/date in UTC format in a string?

example: 例:

$date = 'Wed, 18 Feb 2009 16:03:52 GMT'; $ date ='星期三,2009年2月18日16:03:52 GMT';

//How can I get $date to equal the current time in the same format? //如何以相同格式获取$ date等于当前时间?

I assume you mean how to get similar format as that? 我想你是说如何获得类似的格式? As there is no exact match in the predefined date constants, this would do it: 由于预定义的日期常量中不存在完全匹配的内容,因此可以这样做:

$date = gmdate('D, j M Y H:i:s e');

That would return the current date and time in the same format as 'Wed, 18 Feb 2009 16:03:52 GMT'. 这将以与“星期三,2009年2月18日16:03:52 GMT”相同的格式返回当前日期和时间。

EDIT 编辑

GMT and UTC are (in normal cases) completely interchangeable, and as gmdate always returns an GMT/UTC date, you can just use this: GMT和UTC(在通常情况下)是完全可以互换的,并且由于gmdate始终返回GMT / UTC日期,因此您可以使用以下命令:

$date = gmdate('D, j M Y H:i:s').' GMT';

Or, as it turns out, you can replace e with T to get GMT: 或者,事实证明,您可以将T替换为e以获得GMT:

$date = gmdate('D, j M Y H:i:s T');

我不确定我是否完全理解,但是如果您只想转换字符串,则可以使用strtotime()

$date = gmdate(DATE_RFC822);

Look at Carbon - PHP API extension for DateTime (Link - https://github.com/briannesbitt/Carbon ) 查看Carbon-DateTime的PHP API扩展(链接-https: //github.com/briannesbitt/Carbon

$now = Carbon::now('UTC');

You can access individual values as follows 您可以按以下方式访问各个值

$y = $now->year;
$h = $now->hour; // and so no

While you can use format() to get expected date format as follows 虽然您可以使用format()获得预期的日期格式,如下所示

$str_date = $now->format('D, d M Y H:i:s T'); // "Mon, 27 Apr 2015 22:12:30 UTC"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM