是否有一种方法来搜索文本文件的两个数字(日期)之间的数字在php


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

我目前有能力在我的网页上选择一个" to "answers" from "日期:

<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' is 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>

我想知道我是否可以使用from和from框中的条目来搜索文本文件中两者之间的任何日期。我的文本文件看起来像这样(有很多条目)第一列是日期:

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 - 

得到了一个答案:

    // Set $fromdate to the entered value if there was one, otherwise set it to something long ago
$fromdate = $_REQUEST["from"] ? $_REQUEST["from"] : date("Y-m-d", strtotime("-100 years"));
// Set $todate to the entered value if there was one, otherwise set it to tomorrow. There shouldn't be any finished dates in the future!
$todate = $_REQUEST["to"] ? $_REQUEST["to"]: date("Y-m-d", strtotime("+1 day"));
// Remove the hyphens so that our dates are in the same format as the text file and we 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 the $row in an array to write out later
         echo $row;
}
 fclose($handle);
}

答案取决于您是否能够保证文本文件中的日期始终按特定顺序排序。一种方法是逐行读取文件,直到找到匹配的开始日期,并继续存储连续的行,直到到达结束日期或文件的末尾。如果文件没有排序,另一种方法是将文件中的所有行加载到数组中并按日期排序。

排序文件方法

$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++;
}
// do whatever with $data here

未排序文件方法

$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;
});
// Anything left in $data at this point is between $to and $from