problems with strtotime 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

problems with strtotime function

Post by eon201 »

Im having trouble getting the the correct answer when I take one strored time away from another. Can anyone see why??

Here is the code:

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;
            $n2 =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
                            
                            
                            $log_time = strtotime($log_time);
                            $mylogtime = $log_time - $log_time[$key - 1];
                            
                            $find = 'booked/excursions/index.php';
                        
                            if (strpos($request, $find) == true)
                            {
                            print_r("$request  ");
                            $n++;
                            echo "$log_time - This is the whole number<br/>";
                            echo "$mylogtime - This should not be the whole number but the sum.<br/><br/>";
                                                
                                                if (($mylogtime) > (420)){
                                                $n2++;
                                                }


                            }
                            
                    
                    else
                    {
                    }

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

    ?>
Here is the output of the file:

/booked/excursions/index.php 1194941763 - This is the whole number
1194941763 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194953776 - This is the whole number
1194953776 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194954604 - This is the whole number
1194954604 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194954612 - This is the whole number
1194954612 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194955060 - This is the whole number
1194955060 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194956424 - This is the whole number
1194956424 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194957320 - This is the whole number
1194957320 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194957433 - This is the whole number
1194957433 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194957902 - This is the whole number
1194957902 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194958178 - This is the whole number
1194958178 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194958543 - This is the whole number
1194958543 - This should not be the whole number but the sum.

/booked/excursions/index.php 1194959837 - This is the whole number
1194959837 - This should not be the whole number but the sum.


Warning: strtotime() [function.strtotime]: Called with an empty time parameter. in D:\home\Default\planetcruise-svr.co.uk\htdocs\pcm\logreader4.php on line 37

Page views for 'booked/excursions/index.php' = 12


Unique page views for 'booked/excursions/index.php' = 12


I just cant see why the stored numbers dont do the sum. Can anyone else??

Thanks in advance. EOn201
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Is that an array?

Post by churt »

The variable $log_time is not an array. I'm not an expert so I may be missing something but in order to get the previous time I believe you would need to reference the $content array. I think the following code would fix but not sure about the first time through the loop.

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;
            $n2 =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
                            $bits2=explode(" ", $content[$key -1]);
                           
                            $log_time = strtotime($log_time);
                            $mylogtime = $log_time - strtotime($bits2[1]);
                           
                            $find = 'booked/excursions/index.php';
                       
                            if (strpos($request, $find) == true)
                            {
                            print_r("$request  ");
                            $n++;
                            echo "$log_time - This is the whole number<br/>";
                            echo "$mylogtime - This should not be the whole number but the sum.<br/><br/>";
                                               
                                                if (($mylogtime) > (420)){
                                                $n2++;
                                                }


                            }
                           
                   
                    else
                    {
                    }

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

    ?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

file() may be of use here as well.

Perhaps you can do this (untested)...

Code: Select all

<?php
// This doesn't seem to be right to me
$filename = 'logs/'. 'ex'. date('ymd'). '.log'; // Filename Checker

$content = file($filename); // Read the log into an array
$n =0;
$n2 =0;
$current = array();
$previous = array();
foreach ($content as $key => $value) {
    if ($key > 0) {
        $current = explode(" ", $value); // This makes another array from log line
        $previous = explode(" ", $content[$key-1]);  
        $request = $current[5]; // Get cs uri stem
        $log_client = $current[9]; // Get client ip
        $log_time = strtotime($current[1]); // Get client time
        $prev_log_time = strtotime($previous[1]);
        $mylogtime = $log_time - $prev_log_time;
        $find = 'booked/excursions/index.php';
        if (strpos($request, $find) == true) {
           echo $request, '   ';
           $n++;
           echo "$log_time - This is the whole number<br/>";
           echo "$mylogtime - This should not be the whole number but the sum.<br/><br/>";
           if (($mylogtime) > (420)) {
              $n2++;
           }
       } else { }
    }
}       
echo "<br/>Page views for '$find' = $n<br/><br/>";
echo "<br/>Unique page views for '$find' = $n2<br/><br/>";
?>
Post Reply