Page 1 of 1

Doing math with the date function

Posted: Mon Nov 12, 2007 9:27 am
by eon201
Hi im kinda stuck with the date function.

What im trying to do is to get my code to compare two date times (a and b). And if a is >7 minutes in time difference then continue with the if statement if else ignore.
But im stuc and dont know how to do this. Any help will be greatly appreciatted.

Code: Select all

<?php

$filename = 'logs/'. 'ex'. date('ymd'). '.log'; // Filename Checker

		$fp = fopen($filename, "r"); //Open the server log
        $content = fread($fp, filesize($filename));	 // Read the server log	
		$content = explode("\n", $content); // explode into array	
		$content  = array_reverse($content ); // reverse the array
		$n =0;
		foreach ($content as $key=>$value)
		{
			

						$bits = explode(" ", $value);
						@$request = $bits[5]; // Get cs uri stem
						@$log_client = $bits[9]; // Get client ip
						@$log_time = $bits[1]; // Get client time
						
						$mylogtime = $log_time;
												
						$find = 'booked/excursions/index.php';
						
						if (strpos($request, $find) == true) //SURELY IN HERE I SHOULD USE $array[$key - 1]?? comparing $mylogtime and $log_time ??
						{
						print_r("$request  ");
						$uniquepages[] = $log_client;
						$n++;
						echo "$mylogtime<br/>";
						}
						
						

				
				
				else
				{
				}

		}		
				echo "<br/>Page views for '$find' = $n<br/><br/>";
				echo "Unique page views =". count(array_unique($uniquepages));
				fclose($fp);			

?>
Thanks in advance. EON201

Posted: Mon Nov 12, 2007 9:41 am
by s.dot

Code: Select all

$mylogtime = $log_time;
                                    
                  $find = 'booked/excursions/index.php';
                  
                  if (strpos($request, $find) == true) //SURELY IN HERE I SHOULD USE $array[$key - 1]?? comparing $mylogtime and $log_time ??
If you compare them, they will always be the same, since you set them equal to each other.

Comparing times is easier than comparing dates. You might want to look at strtotime() to convert dates to timestamps.

Then the 7 minute comparison is easy.

Code: Select all

if (($timestampNewest - $timestampOldest) > (7*60))
{
    //7 minutes + difference
}

Posted: Mon Nov 12, 2007 10:15 am
by eon201
For some reason I am getting this error 'Warning: strtotime() [function.strtotime]: Called with an empty time parameter'???

my code currently looks like this:

Code: Select all

$bits = explode(" ", $value);
						@$request = $bits[5]; // Get cs uri stem
						@$log_client = $bits[9]; // Get client ip
						@$log_time = $bits[1]; // Get client time
						
					//	$mylogtime = $log_time;
						$log_time = strtotime($log_time);					
						$find = 'booked/excursions/index.php';
						
						if (strpos($request, $find) == true)
						{
						print_r("$request  ");
						$uniquepages[] = $log_client;
						$n++;
						echo "$log_time<br/>";
						}
What am I doing wrong??

Thanks again. Eon201

Posted: Mon Nov 12, 2007 1:10 pm
by John Cartwright
Remove the error supression (the @ symbols) from your code. Typically you should never have to use it, using isset() is much cleaner.

try adding

Code: Select all

var_dump($log_time);
to see what the variable contains.

Code: Select all

$fp = fopen($filename, "r"); //Open the server log
$content = fread($fp, filesize($filename));    // Read the server log 
$content = explode("\n", $content); // explode into array
Can also be replaced with

Code: Select all

$content = file($filename);