Easy RFC822 formatted dates with PHP
Posted on Thursday, May 03rd (4036 days ago)
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:
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 designer/developer who makes stuff for screens of all shapes and sizes. He currently works at Edvisors, Inc. where he creates screen-based experiences for used by millions of college students every year.