RFC3339 in PHP

Tags: , March 9, 2006 (5 comments)

This is deprecated, and has known bugs. See here for a replacement.

Having searched around for any function to parse RFC3339 dates (used in Atom) in PHP, and failing to find any decent one, I wrote my own. In short, all it does is rearrange the date to a format strtotime() understands.

<?phpfunction parse_date($date)
{
    if (
preg_match('/([0-9]{2,4})-([0-9][0-9])-([0-9][0-9])T([0-9][0-9]):([0-9][0-9]):([0-9][0-9])(\.[0-9][0-9])?Z/i'$date$matches))
    {
        if (isset(
$matches[7]) && substr($matches[7], 1) >= 50)
            
$matches[6]++;
        return 
strtotime("$matches[1]-$matches[2]-$matches[3] $matches[4]:$matches[5]:$matches[6] -0000");
    }
    else if (
preg_match('/([0-9]{2,4})-([0-9][0-9])-([0-9][0-9])T([0-9][0-9]):([0-9][0-9]):([0-9][0-9])(\.[0-9][0-9])?(\+|-)([0-9][0-9]):([0-9][0-9])/i'$date$matches))
    {
        if (isset(
$matches[7]) && substr($matches[7], 1) >= 50)
            
$matches[6]++;
        return 
strtotime("$matches[1]-$matches[2]-$matches[3] $matches[4]:$matches[5]:$matches[6] $matches[8]$matches[9]$matches[10]");
    }
    else
    {
        return 
strtotime($date);
    }
}
?>

I actually wrote this for SimplePie, and like the rest of SimplePie, is released under the Creative Commons Attribution License 2.5 it is released under the zlib/libpng license.

I have nothing to do with SimplePie.

Tags: , January 8, 2006 (0 comments)

Despite what Ryan claims, I have nothing to do with SimplePie 1.0 Preview Release, if you discount the few hundred lines of code I wrote.

Oh, and maybe I should say Happy New Year, just to seem polite :D

Page:  1 2