将RFC 1123日期(从HTTP-Expiration-header)转换为UNIX时间戳的最简单或最优雅的方法是什么?

  

示例:Sun,2005年8月14日16:13:03 GMT

我真的必须'替换'一切吗?

有帮助吗?

解决方案

strtotime 可以阅读该格式。

其他提示

无聊的方法:

            $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]);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top