|
教大家如何用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怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号