views:

325

answers:

2

What is the easiest or most elegant way to convert a RFC 1123 date (From an HTTP-Expiration-header) to a UNIX timestamp?

Example: Sun, 14 Aug 2005 16:13:03 GMT

Do I really have to 'substr' everything?

A: 

The boring method:

            $datestring = 'Sun, 14 Aug 2005 16:13:03 GMT';
            $months = array('Jan' => 1,
                            'Feb' => 2,
                            'Mar' => 3,
                            'Apr' => 4,
                            'May' => 5,
                            'Jun' => 6,
                            'Jul' => 7,
                            'Aug' => 8,
                            'Sep' => 9,
                            'Oct' => 10,
                            'Nov' => 11,
                            'Dec' => 12,
                        );

            $date = explode(' ', $datestring);

            // Validity check
            if(count($date) != 6) { return; }

            $time = explode(':', $date[4]);

            // Validity check
            if(count($time) != 3) { return; }                
            if(! isset($months[$date[2]])) { return; }

            // Convert to time
            $timestamp= gmmktime($time[0], $time[1], $time[2], $months[$date[2]], $date[1], $date[3]);
christian studer
+2  A: 

strtotime can read that format.

Gumbo
Thanks. Umm, can't believe I didn't try that one first, strtotime is almost magical...
christian studer