Doing math with the date function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eon201
Forum Newbie
Posts: 10
Joined: Thu Nov 01, 2007 3:57 am

Doing math with the date function

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
eon201
Forum Newbie
Posts: 10
Joined: Thu Nov 01, 2007 3:57 am

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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);
Post Reply