Craig
Craig

Reputation: 69

Comparing server log dates in PHP

I'm looking to write a simple program that will tell me the last time a search engine visited any page on my site by looking at the server logs.

For example I may have:

$date1 = 27/Jan/2013:19:17:12 -0700
$date2 = 27/Feb/2013:19:17:12 - 0700

I want to know that the last time was $date2 - how do I make PHP understand the date format? Do I first need to convert that into a format that PHP understands?

Thanks in advance for any help.

Craig

Upvotes: 0

Views: 43

Answers (2)

alwaysLearn
alwaysLearn

Reputation: 6950

strtotime() will do your purpose .. It will convert the date in string to timestamp and then uou can do the comparison.

<?php

$date1 = '27/Jan/2013:19:17:12 - 0700';
$date2 = '27/Feb/2013:19:17:12 - 0700';
if(strtotime($date1) > strtotime($date2))
 echo "$date1 is after $date2";
else
 echo "$date2 is after $date1";

Upvotes: 1

Marko D
Marko D

Reputation: 7576

You can use strtotime() and compare timestamps

$date1 = '27/Jan/2013:19:17:12 - 0700';
$date2 = '27/Feb/2013:19:17:12 - 0700';
if(strtotime($date1) > strtotime($date2))
    echo $date1;
else
    echo $date2;

// outputs 27/Feb/2013:19:17:12 - 0700

Upvotes: 1

Related Questions