65

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?

1

5 Answers 5

81

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');
5
  • But the prevalent number of other date string on that page you are referencing to are using the GMT format (7 examples use GMT and just 1 example +0000). And, for example, the USA Today site is responding with the following headers: Last-Modified: Tue, 14 Jan 2014 18:48:38 GMT; Expires: Tue, 14 Jan 2014 18:50:58 GMT; Date: Tue, 14 Jan 2014 18:50:38 GMT, so all are using GMT. Jan 14, 2014 at 18:54
  • 2
    GMT is Greenwich Mean Time it's absolute. Jan 14, 2014 at 18:58
  • 1
    Ok, apparently gmdate("D, d M Y H:i:s T", 123456) is producing strings with GMT, so thanks anyways. Jan 14, 2014 at 19:15
  • You are citing the wrong spec. What's relevant is greenbytes.de/tech/webdav/rfc2616.html#rfc.section.3.3.1 (or the spec that's going to replace it: greenbytes.de/tech/webdav/…) Jan 14, 2014 at 19:37
  • 1
    DATE_RFC7231 constant added in PHP 7.0.19, in this commit
    – sudoqux
    Feb 20, 2019 at 10:45
21

Well, let's have a look at RFC 2616 which defines HTTP 1.1: https://www.rfc-editor.org/rfc/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');
2
  • There is a constant for the preferred format DateTime::RFC1123/DATE_RFC1123. May 2, 2016 at 21:23
  • 5
    That’s close but not the same. It uses O in the end instead of T, which will return +0000 instead of GMT (as required by the standard).
    – hanzi
    May 6, 2016 at 19:19
4

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
4

With DateTime extension, you can use the the RFC7231 constant which refer to the RFC7231 Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content, chapter 7.1.1.1 Date/Time Formats

$httpDate = $dateTime->format(DateTimeInterface::RFC7231);

The constant value is actually the same string as proposed in other answers here D, d M Y H:i:s \G\M\T

-3

Via Carbon:

Carbon::now()->setTimezone('GMT')->format("D, d M Y H:i:s T")
1
  • Please add some explanation to your answer. The question is "Which format is used", not how to generate it
    – Nico Haase
    Dec 3, 2020 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.