Easy RFC822 formatted dates with PHP

I love PHP.  It’s THE server-side language for the web as far as I’m concerned.  Especially when it comes to working with strings and dates. My old site had an RSS feed for all the little articles I’d add in, and the biggest pain in the butt was getting the date to be properly formatted. Not so in PHP.  Here’s all you have to do:

echo date('r', strtotime($my_date));

To explain this a little, lets start from the very inside and work our way out:

strtotime($my_date);

This turns the datetime from inside our MySQL table which is formatted like: YYYY-MM-DD HH:MM:SS and turns it into a UNIX string that represents the same date.

Once we do that to our date, we have the date() function’s many wonders at our disposal.  Going into those many wonders is more than I’ll get into here, but I’ve posted about it here.

In this particular case, adding the ‘r’ parameter to the date() function does all the work of reformatting our UNIX string into our RSS feed date format.

It’s that easy.

 


About Joseph R. B. Taylor

Joseph R. B. Taylor is a humble designer/developer who makes stuff for screens of all shapes and sizes. He is currently the lead UI/UX Architect at MScience, LLC, where he works to create simple experiences on top of large rich datasets for their customers and clients.