php 符合apache日誌日期的程式碼

WBOY
發布: 2016-07-25 08:56:57
原創
820 人瀏覽過
教大家如何用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);
?>
登入後複製


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!