kassner / log-parser

PHP Web Server Log Parser Library
Apache License 2.0
334 stars 64 forks source link

How to print log entries between 2 dates? #67

Closed s22-tech closed 3 weeks ago

s22-tech commented 1 month ago

Is there a built-in way to print log entries between 2 dates, e.g. to show data for the week of July 28th, from Sunday to Saturday only? Thanks!

kassner commented 3 weeks ago

You can either filter that beforehand with any text-parsing tool like grep, although it depends on how you obtain your logs, or you can do it in PHP by using the stamp field. Something like:

$from = strtotime('2024-07-21 00:00:00');
$to = strtotime('2024-07-28 23:59:59');

$entry = $parser->parse($logline);
if ($entry->stamp >= $from && $entry->stamp <= $to) {
  // do something
}