Get MySQL date in RFC-822 format for RSS feeds

Home -> Database -> MySQL

7831 views

From the computer of: qmchenry (337 recipes)
Created: Jun 19, 2006


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

To get the PubDate element of an RSS feed to validate, it needs to be in RFC-822 format. MySQL has flexible ways of working with dates and times which make this a simple task.

Given a datetime column called pubdate, this select statement (which can be combined with other selections) will yield a column of dates named rfcpubdate formated in RFC-822 format:

SELECT DATE_FORMAT(pubdate,'%a, %d %b %Y %T') AS rfcpubdate FROM tablename WHERE 1


The full RFC-822 date includes a timezone value which is not included in the MySQL output. If using PHP, this value can be appended to the output (assuming a variable $rfcpubdate exists containing a date string from the above select statment) using:

echo "<pubDate>$rfcpubdate ".date('T')."</pubDate>";


This will yield output that looks like:

<pubDate>Mon, 19 Jun 2006 07:41:18 PDT</pubDate>

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  MySQL - Daily database dumps, all nicely sorted.
  Display a list of databases on a MySQL server
  Solve: Can't connect to local MySQL server through socket /tmp/mysql.sock
  Connect to a MySQL server using the mysql command
  Create a basic MySQL table
  Determine the version of MySQL server
  Describe the column structure of a MySQL table
  Create a MySQL database
  Select a MySQL database to use
  Drop or delete a table in MySQL

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.