从给定的txt文件中,查出需要的记录,使用php语言

来源:百度知道 编辑:UC知道 时间:2024/05/27 01:18:49
现在有一个a.txt,内容如下:
12:52, 24th May as 1 guest
12:53, 24th May eee sss guest
19:13, 24th May K guest
19:13, 25th May P guest

主要就是根据这个时间来查找记录,使用php语言,不知道能否实现~
如果我在前台页面上输入的时间是5月25号,程序就查出来上面最后一条,然后在页面上显示出来~求教
我前台输入的应该是
2009-05-25这样的格式~

要修改下,在txt里面保存的格式是
2009-04-01
2009-04-02
2009-04-03
这个样子的~

主要思路就是转换成时间戳进行比较,这样技能适应不同的格式,又比较方便。里边有详细的解释,希望能对你能有所帮助
<?php

$date = "2009-05-25";
#转换成时间戳,以便比较
$date = strtotime($date);
echo text_query($date);

function text_query($date){
$f = fopen("log.txt", 'rb');
$year = date("Y");
while(!feof($f)){
$line = fgets($f);
#取出时间值
$timearr = split(" ", $line, 4);
#将时间改成0:00,也就是值判断日期
$timearr[0] = "00:00";
#将年换成今年
$timearr[3] = $year;
#转换成时间戳
$time = strtotime(join(" ", $timearr));
if($date == $time){
return $line;
}

}
}

程序如下:

<?php
foreach (file('a.txt') as $line)
if (strpos($ine,',25th May ')) echo $line;
?>