4

I have a file with the following lines. I want to identify lines which have timestamps less than a week from now(starting from aug 22 for this example).

log3.txt
28-08-2011 10:29:25 A string
29-08-2011 14:29:25 A new string
20-08-2011 14:29:25 Don’t include

php file

if($file = fopen("/opt/apache2/htdocs/log3.txt", "r"))
{
    while(!feof($file))
    {
        $contents = fgets($file, 23); // This is supposed to be 22.
        echo $contents; // With 22 it only returns 08-29-2011 14:29:25 P
        if(strtotime($contents) > strtotime("last Monday"))
        {
            $string1 = fgets($file);
            echo "In if $string1";
            // Do something
        }
        else
        {
            $string1 = fgets($file);
            echo "In else $string1"; //Always goes to else statement.
        }
    }
}

The timestamps are created with date('m-d-Y H:i:s').

Why does fgets need 23 characters when it's actually 22.

What am I doing wrong here with strtotime?

What is the best way to parse lines with timestamps less than a week ago.

EDIT: Got it to work

$date = DateTime::createFromFormat('d-m-Y H:i:s', $contents);

if(strtotime($date->format('d-m-Y H:i:s')) > strtotime("last Monday"))

Use DateTime function with d-m-Y format or m/d/Y.

Thanks for all the help guys.

theking963
  • 2,223
  • 7
  • 28
  • 42

5 Answers5

4

Use DateTime::createFromFormat instead. strtotime() may have problems to detect the used format of your date.

$date = DateTime::createFromFormat('d-m-Y H:i:s A', $contents);
str
  • 42,689
  • 17
  • 109
  • 127
1

The length parameter of fgets() includes the newline character (\n), that's why have to pass 22+1 to this paramter.

Using fread() would be more appropriate for this case.

For the strtotime:

strtotime does not recognizes your date format, so you have to parse it in using an other function. strptime can do it:

strptime('08-29-2011 10:29:25 AM', '%m-%d-%Y %H:%M:%S %p')
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
0

I don't like it when strtotime() gets thrown under the bus. It isn't evil. It has great flexibility, but it can only go so far.

$contents just needs to be adjusted so that strtotime() understands you are feeding it a European-style datetime. You don't even need to swap around any digits.

Use:

if(strtotime(str_replace("/","-",$contents)) > strtotime("last Monday")){

This is a simpler fix than instantiating an new object for each iteration.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

To quote from the manual page:

Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.

As for the conversation, I'd use strptime(), as your date format is fixed:

if(strptime($contents, 'm-d-Y H:i:s A') > strtotime("last Monday"))

If this doesn't help you, please var_dump() the two strtotime() and post the results.

Lars
  • 5,757
  • 4
  • 25
  • 55
0

Your dates are not the right format for strtotime to return a timestamp:

$dt = strtotime("08-29-2011 10:29:25 AM");
var_dump($dt); // false

Update
But this works for me:

$lm = strtotime('last Monday');
$dt = strtotime('23-08-2011');

if($dt > $lm) {
   echo "In if"; // output for dates earlier than 23-08 
} else {
   echo "In else"; // output for 23-08 and later
}
Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127
  • You are right. It's supposed to be in `28-08-2011` or `08/28/2011`. I fixed that but it's the still the same. – theking963 Aug 29 '11 at 16:21
  • Yes that works individually. Put the date in another file and use fgets/fread to get the date and it doesn't work. – theking963 Aug 29 '11 at 16:53