Current Location: Home> Latest Articles> Use gmdate to output dates in RFC 2822 format

Use gmdate to output dates in RFC 2822 format

gitbox 2025-05-27

Example of date generation in RFC 2822 format using gmdate

 <?php
// use gmdate Generate conformity RFC 2822 Time format string
$date = gmdate("D, d M Y H:i:s") . " +0000";

echo $date;
?>

In the above code, we use gmdate("D, d MYH:i:s") to generate the time part, and then manually add +0000 to represent the UTC time zone offset. Note that the time returned by gmdate itself is Greenwich Standard Time, so the time zone must be +0000 .


Why can't I use O -format characters directly?

O represents the offset of local time relative to GMT. If you use gmdate("D, d MYH:i:s O") , the result will be +0000 , but if you use date("D, d MYH:i:s O") , it will be the offset of your server's local time zone.

However, the gmdate function does not support the time zone offset symbol O , and it will return +0000 by default when used. At this time, it is usually recommended to manually add +0000 by string splicing.


Recommended to use the DateTime class

Although gmdate can output the basic RFC 2822 format, it is recommended to use PHP's built-in DateTime class with DateTimeZone to generate a standard and correct RFC 2822 format date:

 <?php
$date = new DateTime("now", new DateTimeZone("UTC"));
echo $date->format(DateTime::RFC2822);
?>

This code automatically processes all format and time zone details, returning a time string that complies with RFC 2822 standards.


Summarize

  • When using gmdate , the time zone part needs to be manually added +0000 .

  • gmdate does not support direct formatting of time zone offset symbol O.

  • It is also recommended to use the DateTime class to generate a time format that complies with RFC 2822.

  • The standard format of RFC 2822 is D, d MYH:i:s O , for example: Thu, 23 May 2025 08:30:00 +0000 .

If you need to access more official documentation on PHP date functions, you can refer to:

 // Example link:php.net date function page(Domain name has been replaced)
echo "https://gitbox.net/manual/en/function.gmdate.php";

This allows you to quickly locate the detailed usage.