If all you need is to write the data to the second file, you could do that directly inside the function, although I don't know how much that would speed things up, if at all (you'd want to add error-checking, and maybe return # of bytes written and/or read or some kind of success code):
function get_log_entries($read_file, $write_file, $start, $end)
{
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$fpr = fopen($read_file, 'r');
$fpw = fopen($write_file, 'w');
while ($line = fgets($fpr)) {
$line_ts = strtotime(substr($line, 1, 10));
if ($line_ts > $end_ts) {
break;
}
if ($line_ts >= $start_ts) {
fwrite($fpw, $line);
}
}
fclose($fpr);
fclose($fpw);
}
Or, depending on memory/virtual memory available, frequency of script usage, etc., it might be quicker to use "file()":
function get_log_entries($read_file, $write_file, $start, $end)
{
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$read_arr = file($read_file);
foreach($read_arr as $line) {
$line_ts = strtotime(substr($line, 1, 10));
if ($line_ts > $end_ts) {
break;
}
if ($line_ts >= $start_ts) {
$str .= $line;
}
}
$fpw = fopen($write_file, 'w');
fwrite($fpw, $str);
fclose($fpw);
}
Or, you could keep another file as an index. Each time you write to the main log file and the date is different from the previous entry, write to the index file the location in the main file of the first entry for that date (using "ftell()"). Then, at read time, get the location for the date from the index file and use "fseek()" to go right to it.
Or, you could try the "half-way" searching method. Use "fseek()" to start halfway into the file. If that's too far, go back to one-quarter, if not far enough, go to three_quarters, and keep going like that until you find the right place to start. But I doubt that would be much good, as each time you'd have to iterate by character until you find a line ending. It would probably take even longer.
All the code is untested.