PHP timestamp to pubDate style in RSS

I created RSS from mysql database using php.
Here is some notes for myself in the future if I have to troubleshoot again.

  • You can make PHP file produce xml by putting this in the beginning of the code,

    <?php header('Content-type: text/xml', true);
    echo '<?xml version="1.0" encoding="utf-8"?'.'>'; ?>

  • Check it using W3C Feed Validation Service
  • pubDate needs to be RFC-822 date-time. My database stored the time in regular timestamp in yyyy-mm-dd hh:mm:ss, such as 2009-11-27 20:00:17.

    All I had to do was this: ($myTime is “2009-11-27 20:00:17”)

    $date_rfc822 = date(r,$myTime);

    From w3schools PHP date() Function page
    MY MISTAKE: FIxed it here. above should be $date_rfc822 = date(r,strtotime($myTime));

  •’s Encode / Decode HTML Entities is awesome.
  • Some php functions that I have used for this project:
    preg_replace Regular expression to replace string… I need to study this more. Here is one reference.
    trim remove unwanted spaces form the string.
    stripos Check to see if a phrase/word exist in a string


function clean_input($text)
$text = strip_tags($text);
$text = stripslashes($text);
$text = preg_replace('/\s\s+/', ' ', $text);
$text = trim($text);
return $text;

$myText = mysql_real_escape_string($strText,$db_handle);
$modDate = strtotime($strDate);

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>