file: data.txt (11617 lines)
user datetime
23 2015-03-01 08:04:15
15 2015-05-01 08:05:20
105 2015-05-01 08:07:10
15 2015-06-01 08:08:29
105 2015-06-01 08:12:48
I only need data in 2015-06, I'm using fget and check each line's datetime but really slow, more than 50s.
$d='data.txt';
import($d);
function import($d){
$handle = fopen($d, "r") or die("Couldn't get handle");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$line=explode("\t",$buffer);
if(date("Y-m",strtotime($line[1])=="2015-06"){
mysql_query("INSERT INTO `table` ....");
}
else{
//break? when month>6
}
}
fclose($handle);
}
}
SOLUTION: less than 2s!!!! (thanks to Kevin P. and Dragon)
if(substr($line[1],0,7)=="2015-06"){
$sql.=empty($sql)?"":","."(`".$line[1]."`.........)";
}
elseif(substr($line[1],0,7)>"2015-06"){
break;// when month>6
}
mysql_query("INSERT INTO `table` ....".$sql);