php HTTP 标头中使用的日期/时间格式

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

The date/time format used in HTTP headers

phphttpdaterfc

提问by Desmond Hume

Which RFC describes the format used for date/time in the modern time HTTP headers, like "Last-Modified" and "If-Modified-Since", and how to generate a date/time string in PHP according to such format?

哪个 RFC 描述了现代 HTTP 标头中用于日期/时间的格式,例如“Last-Modified”和“If-Modified-Since”,以及如何根据这种格式在 PHP 中生成日期/时间字符串?

Some sources point to RFC 2822, which, as indicated by DateTimeclass, is using D, d M Y H:i:s Oformat, but from my tests, this format produces +0000instead of GMTat the end. I tried other timezone specifiers but none of them seems to put GMTat the end, the closest result I got was with UTC. However, as was shown by Firebug, all sites are using GMTin HTTP headers and not +0000or UTC.

一些来源指向 RFC 2822,正如DateTime类所指示的那样,它正在使用D, d M Y H:i:s O格式,但从我的测试来看,这种格式生成+0000而不是GMT最后。我尝试了其他时区说明符,但似乎没有一个放在GMT最后,我得到的最接近的结果是UTC. 但是,正如 Firebug 所示,所有站点都GMT在 HTTP 标头中使用,而不是使用+0000UTC

So what format is really used and how do I format date/time in the same way as other sites do?

那么真正使用的是什么格式,我如何以与其他网站相同的方式格式化日期/时间?

回答by Glavi?

As you can see here, Last-Modifiedheader has datetimes in RFC2616format.

正如您在此处看到的,Last-Modified标头具有RFC2616格式的日期时间

In section 14.29 Last-Modifiedyou can see that date format should be:

在部分中,14.29 Last-Modified您可以看到日期格式应为:

"Last-Modified" ":" HTTP-date

An example of its use is

它的使用示例是

Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT

Another quote from RFC2616 read more:

RFC2616 的另一个引用阅读更多

All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception.

所有 HTTP 日期/时间戳都必须以格林威治标准时间 (GMT) 表示,无一例外。

In PHP you can use format D, d M Y H:i:s Tif you use function gmdate()which always returns datetime in GMT offset/timeszone:

在 PHP 中,D, d M Y H:i:s T如果您使用gmdate()始终以 GMT 偏移量/时区返回日期时间的函数,则可以使用格式:

echo gmdate('D, d M Y H:i:s T');

If you wish to use DateTimeextension:

如果你想使用DateTime扩展:

$dt = new DateTime('UTC');
#$dt = new DateTime('2013-01-01 12:00:00', new DateTimezone('UTC'));
echo $dt->format('D, d M Y H:i:s \G\M\T');

回答by hanzi

Well, let's have a look at RFC 2616 which defines HTTP 1.1: http://tools.ietf.org/html/rfc2616#section-3.3

好吧,让我们看看定义 HTTP 1.1 的 RFC 2616:http: //tools.ietf.org/html/rfc2616#section-3.3

HTTP applications have historically allowed three different formats for the representation of date/time stamps:

 Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
 Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
 Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format

The first format is preferred as an Internet standard and represents a fixed-length subset of that defined by RFC 1123 [8] (an update to RFC 822 [9]).

(...)

All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception.

HTTP 应用程序历来允许使用三种不同的格式来表示日期/时间戳:

 Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
 Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
 Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format

第一种格式是首选的 Internet 标准,代表 RFC 1123 [8](对 RFC 822 [9] 的更新)定义的固定长度子集。

(……)

所有 HTTP 日期/时间戳都必须以格林威治标准时间 (GMT) 表示,无一例外。

So DateTime::COOKIEor Datetime::RFC850use a valid format. The preferred one according to the RFC would be D, d M Y H:i:s Twhich is not defined by any constant in the DateTimeclass.

所以DateTime::COOKIEDatetime::RFC850使用有效的格式。根据 RFC 的首选D, d M Y H:i:s T是不由DateTime类中的任何常量定义的。

To make sure that GMT is used, the following code should suffice:

为了确保使用 GMT,以下代码就足够了:

gmdate('D, d M Y H:i:s T');

回答by Fraser

I'm pretty sure the (now) correct answer here is rfc7231 - section 7.1.1.1It specifies Date/Time Formats and is where the HTTP-datesemantics are defined.

我很确定这里的(现在)正确答案是rfc7231 - 第 7.1.1.1 节它指定了日期/时间格式并且是HTTP-date定义语义的地方。

HTTP-date    = IMF-fixdate / obs-date

Also we can see that

我们也可以看到

When a sender generates a header field that contains one or more timestamps defined as HTTP-date, the sender MUSTgenerate those timestamps in the IMF-fixdateformat.

当发送方生成包含一个或多个定义为HTTP-date 的时间戳的标头字段时,发送方必须IMF-fixdate格式生成这些时间戳。

So for a server sending a "modern time HTTP header"- where the value is a HTTP-datethe format is equivalent to the IMF-fixdateformat.

因此,对于发送“现代 HTTP 标头”的服务器 - 其中值是HTTP-date格式等同于IMF-fixdate格式。

So to answer the actual question.

所以要回答实际问题。

Which RFC describes the format used for date/time in the modern time HTTP headers

哪个 RFC 描述了现代 HTTP 标头中用于日期/时间的格式

You need to know the definition of IMF-fixdate- which is in rfc7231. It also give the definition of obs-datetoo i.e rfc850-date/ asctime-date

您需要知道IMF-fixdate- 在 rfc7231 中的定义 。它还给出了obs-date太 ie rfc850-date/的定义asctime-date

     IMF-fixdate  = day-name "," SP date1 SP time-of-day SP GMT
     ; fixed length/zone/capitalization subset of the format
     ; see Section 3.3 of [RFC5322]

     day-name     = %x4D.6F.6E ; "Mon", case-sensitive
                  / %x54.75.65 ; "Tue", case-sensitive
                  / %x57.65.64 ; "Wed", case-sensitive
                  / %x54.68.75 ; "Thu", case-sensitive
                  / %x46.72.69 ; "Fri", case-sensitive
                  / %x53.61.74 ; "Sat", case-sensitive
                  / %x53.75.6E ; "Sun", case-sensitive


     date1        = day SP month SP year
                  ; e.g., 02 Jun 1982

     day          = 2DIGIT
     month        = %x4A.61.6E ; "Jan", case-sensitive
                  / %x46.65.62 ; "Feb", case-sensitive
                  / %x4D.61.72 ; "Mar", case-sensitive
                  / %x41.70.72 ; "Apr", case-sensitive
                  / %x4D.61.79 ; "May", case-sensitive
                  / %x4A.75.6E ; "Jun", case-sensitive
                  / %x4A.75.6C ; "Jul", case-sensitive
                  / %x41.75.67 ; "Aug", case-sensitive
                  / %x53.65.70 ; "Sep", case-sensitive
                  / %x4F.63.74 ; "Oct", case-sensitive
                  / %x4E.6F.76 ; "Nov", case-sensitive
                  / %x44.65.63 ; "Dec", case-sensitive
     year         = 4DIGIT

     GMT          = %x47.4D.54 ; "GMT", case-sensitive

     time-of-day  = hour ":" minute ":" second
                  ; 00:00:00 - 23:59:60 (leap second)

     hour         = 2DIGIT
     minute       = 2DIGIT
     second       = 2DIGIT

   Obsolete formats:

     obs-date     = rfc850-date / asctime-date

     rfc850-date  = day-name-l "," SP date2 SP time-of-day SP GMT
     date2        = day "-" month "-" 2DIGIT
                  ; e.g., 02-Jun-82

     day-name-l   = %x4D.6F.6E.64.61.79    ; "Monday", case-sensitive
            / %x54.75.65.73.64.61.79       ; "Tuesday", case-sensitive
            / %x57.65.64.6E.65.73.64.61.79 ; "Wednesday", case-sensitive
            / %x54.68.75.72.73.64.61.79    ; "Thursday", case-sensitive
            / %x46.72.69.64.61.79          ; "Friday", case-sensitive
            / %x53.61.74.75.72.64.61.79    ; "Saturday", case-sensitive
            / %x53.75.6E.64.61.79          ; "Sunday", case-sensitive


     asctime-date = day-name SP date3 SP time-of-day SP year
     date3        = month SP ( 2DIGIT / ( SP 1DIGIT ))
                  ; e.g., Jun  2