Home  >  Article  >  Backend Development  >  php code to match apache log date

php code to match apache log date

WBOY
WBOYOriginal
2016-07-25 08:56:57819browse
Teach you how to use php code to extract the date in the apache log. The code is very simple and suitable for beginners to refer to.

The following code can be used to match the date in the apache log, and then get a return result similar to: 17 Dec 06 03:26:49 -0500.

php gets the date in the apache log, as follows:

<?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);
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn