Formatting a Wordpress post date in the RSS format

Wordpress automatically formats the post_date field in the wp_posts table using various date formats, including the RSS and Atom ones. The default data type for this field is datetime, which means that the date stored in this field has the format YYYY-MM-DD HH:MM:SS. However, sometimes we may need to format this data type directly, for example if something goes wrong with our RSS or Atom feeds and we actually have to manually create a physical feed. Here's how we can do:

$ts = '2011-05-29 00:00:00';
echo date('r', strtotime($ts));

$ts represents a date taken from the Wordpress database (post_date field). We use the PHP date() function in this way:

  1. we use the r parameter to get a valid RSS date format
  2. we use the strtotime() function to create a Unix timestamp from the date string passed to date()

date() works with Unix timestamps, so we actually need to convert the default Wordpress date format before returning a valid RSS date.

Leave a Reply

Note: Only a member of this blog may post a comment.