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.

Comments

  1. Brian Layman
    says

    March 21, 2006 12:34:10+00:00

    Hey - I'm just passing through your site (actually I was googling for a vB/BBCode plugin for wordpress and bounced through coding forums to your site) but I noticed that your code syntax highlighting is doing what mine used to - expanding beyond the boundaries (in ie7 at least). Earlier today I found a plugin that fixed that. I'm not a marketer for it, I've only used it for a day, but I thought you might be able to benifit if I shared. You can see some samples of how it works in the post I just submitted before heading off to Google: http://www.thecodecave.com/?p=53

  2. Brian Layman
    says

    March 21, 2006 12:36:33+00:00

    OH! and I meant to say that I really like the color scheme of your site. It has a look I was going for before I switched to my curent theme. The middle column could take more advantage of the width of the browser, but that's a relatively simple CSS change.

    Good job on it!

  3. Geoffrey Sneddon
    says

    March 21, 2006 20:49:24+00:00

    Appears fine for me in IE6 and IE7…

  4. Brian Layman
    says

    March 29, 2006 12:04:18+01:00

    Huh.. This is what I see in IE7 on XPSp2. Most blogs, including my own in the past, do this:

    http://www.thecodecave.com/images/Geoffers.JPG

    Oh, well... If it works fine for you as is, that's cool too...

  5. Geoffrey Sneddon
    says

    March 29, 2006 19:40:50+01:00

    It works for me on my clean install of IE7B2P on XP SP2… I've not had anyone else comment on it, and I know other people do view my blog in IE7, so I'll assume it's fine.

Leave a Reply