Home » Php » php – The date/time format used in HTTP headers

php – The date/time format used in HTTP headers

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

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

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

How to&Answers:

As you can see here, Last-Modified header has datetimes in RFC2616 format.

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

"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 :

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

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

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

If you wish to use DateTime extension:

$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');

Answer:

Well, let’s have a look at RFC 2616 which defines HTTP 1.1:
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.

So DateTime::COOKIE or Datetime::RFC850 use a valid format. The preferred one according to the RFC would be D, d M Y H:i:s T which is not defined by any constant in the DateTime class.

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

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

Answer:

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

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 MUST generate those
timestamps in the IMF-fixdate format.

So for a server sending a “modern time HTTP header”- where the value is a HTTP-date the format is equivalent to the IMF-fixdate format.

So to answer the actual question.

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

You need to know the definition of IMF-fixdate – which is in rfc7231.
It also give the definition of obs-date too i.e 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