>  기사  >  백엔드 개발  >  php 匹配apache日志日期的代码

php 匹配apache日志日期的代码

WBOY
WBOY원래의
2016-07-25 08:56:57820검색
教大家如何用php代码提取出apache日志中日期的方法,代码很简单,适合初学的朋友参考。

以下代码可用于匹配apache日志中的日期,然后得到类似:17 Dec 06 03:26:49 -0500的返回结果。

php得到apache日志中的日期,如下:

<?php
// Set the default timezone to US/Eastern time:
date_default_timezone_set('US/Eastern');

// Simulate reading an Apache log file, with the following line:
$logline = '127.0.0.1 - - [17/dec/2006:00:26:49 -0800] "GET / HTTP/1.1" 200 41228';

// Since we only want the date section, use regex to obtain it:
preg_match('/\[(.*?)\]/', $logline, $matches);

// Take the date, and convert it:
$timestamp = strtotime($matches[1]);

// Now echo it out again to ensure that we read it correctly:
echo date(DATE_RFC822, $timestamp);
?>


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.