is there a way to search a text file a number between two numbers (dates) in php? -


i have ability chose 'to' , 'from' date on webpage:

<script>     $(function() {       if(document.getelementbyid("from").type == "text") {         var maxdate = new date();         var mindate = new date("2012-01-01");         $("#from").datepicker({           changemonth: true,           changeyear: true,           dateformat: "yy-mm-dd",           defaultdate: "+1w",           maxdate: maxdate,           mindate: mindate,           numberofmonths: 1,           showmonthafteryear: true,           showothermonths: true,           selectothermonths: true,           onclose: function(selecteddate) {             $("#to").datepicker("option", "mindate", selecteddate);           }         });         $("#to").datepicker({           changemonth: true,           changeyear: true,           dateformat: "yy-mm-dd",           defaultdate: "+1w",           maxdate: maxdate,           mindate: mindate,           numberofmonths: 1,           showmonthafteryear: true,           showothermonths: true,           selectothermonths: true,           onclose: function(selecteddate) {             $("#from").datepicker("option", "maxdate", selecteddate);           }         });       }       else {         $("#from").on("change", function() {           $("#to").prop("min", $(this).val());         });         $("#to").on("change", function(){           $("#from").prop("max", $(this).val());         });       }     });   </script> <form action="" form method="post">  <input type="checkbox" checked="checked" value="select dates?" name="dates"/>     search between dates? (only available if 'finished' selected)                  <label for="from">from:</label>                 <input id="from" name="from" type="date" value="2015-05-25"/>              </div>                   <label for="to">to:</label>                 <input id="to" name="to" type="date" value="2015-06-24"/> </form> 

i wondering if can use entry , boxes search text file dates between two. text file looks (with lots more entries) first column date:

20150619    finished    17772   resolved    philw       current/17772 -  20150619    finished    17751   resolved    peters      current/17751 -  20150619    finished    17594   resolved    peters      current/17594 -  unfinished  unfinished  16218   open        allanm      current/16218 -  unfinished  unfinished  15918   open        allanm      current/15918 -  

came answer:

    // set $fromdate entered value if there one, otherwise set long ago $fromdate = $_request["from"] ? $_request["from"] : date("y-m-d", strtotime("-100 years"));  // set $todate entered value if there one, otherwise set tomorrow. there shouldn't finished dates in future! $todate = $_request["to"] ? $_request["to"]: date("y-m-d", strtotime("+1 day"));  // remove hyphens our dates in same format text file , can compare them directly $fromdate = str_replace("-", "", $fromdate); $todate = str_replace("-", "", $todate);   $handle = @fopen("project-list.txt", "r");  while(!feof($handle)) {   $row = fgets($handle);  $col1 = explode ("\t", $row);    if(($col1 != "unfinished") && ($col1 >= $fromdate) && ($col1 <= $todate))      // or save $row in array write out later           echo $row;  }   fclose($handle); } 

the answer depends on whether or not can guarantee dates in text file sorted in particular order. 1 way read file line-by-line until find matching start date , continue store consecutive lines until have either reached end date or end of file. other way, if file isn't sorted, load lines file array , sort them date.

sorted file approach

$collect = false; $n       = 1; $file    = 'myfile.txt'; $fp      = fopen($file, 'r'); $data    = [];  while($line = fgets($fp)) {     list($date, $data) = explode("\t", $line, 2); // assumption of tab delimiter     try {         $date = datetime::createfromformat('!ymd', $date);     } catch(exception $e) {         echo "encountered bad date form on line $n in $file!\n";     }     if ($date == $to) {         $collect = true;     }     if ($date == $from) {         break;     }     if ($collect) {         $data[] = $line;     }     $n++; }  // whatever $data here 

unsorted file approach

$filename = 'myfile.txt'; $data = file($filename); $data = array_map(function($line) {           list($date, $data) = explode("\t", $line);           $date = datetime::createfromformat('!ymd', $date);           return ['date' => $date->gettimestamp(), 'line' => $line];         }, $data); usort($data, function($a, $b) {     if ($a['date'] < $b['date']) {         return -1;     } elseif ($a['date'] > $b['date']) {         return 1;     } else {         return 0;     } });  $data = array_filter($data, function($v) use($to, $from) {     return $v['date'] >= $to && $v['date'] <= $from; });  // left in $data @ point between $to , $from 

Comments