Page 1 of 1

Date Conversion from ISO 8601 To RFC 2822 format

Posted: Sat Apr 07, 2007 12:14 pm
by Ambush Commander
Extremely long function name. Quick and dirty.

Code: Select all

function convertDateFromISO8601ToRFC2822($date) {
        preg_match('/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).(\d+)Z/',
            $date, $time_matches);
        list($x, $year, $month, $day, $hour, $minute, $second) =
            $time_matches;
        $timestamp = gmmktime($hour, $minute, $second, $month, $day, $year);
        return gmdate('r', $timestamp);
    }
This got me thinking: there must be a better way to do this! The function only accepts GMT style ISO 8601 dates. I am using this function to convert Subversion timestamps into RSS timestamps.
Example:

Code: Select all

echo convertDateFromISO8601ToRFC2822('1970-01-01T00:00:00Z');
Outputs: Thu, 01 Jan 1970 00:00:00 +0000

Posted: Sat Apr 07, 2007 2:49 pm
by aaronhall
Does strtotime() not understand ISO8601? (I'm too lazy to check myself) -- if not, you could probably replace that "T" with a space and pass that along to strtotime.

Short of that, you could always spell out ISO and RFC in the function name :wink:

Posted: Sat Apr 07, 2007 3:51 pm
by Ambush Commander
It does.

:oops:

I swear, I tested it, and it didn't work! :-/ Oh well, makes my life easier.

Posted: Sat Apr 07, 2007 4:17 pm
by aaronhall
:) PHP does that to me too