I dint know how to title this but...
I`ll start from begining, i have a .txt file loking like this:

2011-11-14 15:39:51,Polnoc
2011-10-13 15:41:54,Polnocny-Wschod
2011-10-14 15:43:04,Wschod

Then there is a code that shows data from last mont,week,day looking like this:

$lines = file('Kier.txt');
$monthlist = "";
$weeklist = "";
$daylist = "";
foreach($lines as $line){
   	$r = explode(",",$line);
   	if(strtotime($r[0]) > strtotime('-1 month'))$monthlist .= "\n\t<li>{$r[1]}</li>";
	if(strtotime($r[0]) > strtotime('-1 week'))$weeklist .= "\n\t<li>{$r[1]}</li>";
	if(strtotime($r[0]) > strtotime('-1 day'))$daylist .= "\n\t<li>{$r[1]}</li>";
}
if($monthlist !="")$monthlist = "\n<ul>$monthlist\n</ul>";
if($weeklist !="")$weeklist = "\n<ul>$weeklist\n</ul>";
if($daylist !="")$daylist = "\n<ul>$daylist\n</ul>";

And a code that caunts all occurences of same data loking like this.

$getText = file_get_contents(monthlist, true);
$Poln = substr_count($getText ,"Polnoc");
$PolnW = substr_count($getText ,"Polnocny-Wschod");
$Wsch = substr_count($getText ,"Wschod");
$PoldW = substr_count($getText ,"Poludniowy-Wschod");
$Pold = substr_count($getText ,"Poludnie");
$PoldZ = substr_count($getText ,"Poludniowy-Zachod"); 
$Zach = substr_count($getText ,"Zachod");
$PolnZ = substr_count($getText ,"Polnocny-Zachod");
$getPoln = $Poln - $PolnW - $PolnZ ;
$getPold = $Pold;
$getZach = $Zach - $PoldZ - $PolnZ ;
$getWsch = $Wsch - $PoldW - $PolnW ;

Now everythig works fine but i need to do it a little diffrent. I need to do two text fields where i will write date from and to. And it should count occurrences only in given time. How to start with this, what should i use, is my code will be helpfull with it, and is it passible anyway??

Member Avatar
diafol

OK, I seem to recognize that code from somewhere! :)

//hard-coded for now - you'd retrieve them from a form, probably with the use of a datepicker.
$datefrom = "2011-06-10";
$dateto = "2011-11-22";

$lines = file('Kier.txt');
$list = "";
foreach($lines as $line){
   	$r = explode(",",$line);
   	if(strtotime($r[0]) >= strtotime($datefrom) && strtotime($r[0]) <= strtotime($dateto))$list .= "\n\t<li>{$r[1]}</li>";
}
if($list !="")$list = "\n<ul>$list\n</ul>";

//not tested

//this may give you everything up to the last day. You may need to use

$dateto = $dateto . " 23:59:59";

to get it to work proberly.

first off really consider starting to use a database such as mysql you can manage data so much better and do so much more with it.

but for the current setup make a new page

<?php
if($_POST['submit'] == 'count'){
 $from = $_POST['from'];
 $to = $_POST['to'];
 $allowedchars = array(':','-',' ','.');
 $testfrom = str_replace($allowedchars,'',$from);
 $testto = str_replace($allowedchars,'',$to);
 if(ctype_digit($textfrom) && ctype_digit($testto)){
  $lines = file('Kier.txt');
  $count = array();
  foreach($lines as $line){
   $r = explode(",",$line);
   if(strtotime($from) < strtotime($r[0]) && strtotime($to) > strtotime($r[0])){
    if(!isset($count[$r[1]]){
     $count[$r[1]] = 1;
    }else{
     $count[$r[1]]++;
    }
  }
  $total = 0;
  echo '<table><tr><td>name</td><td>count</td></tr>';
  foreach($count as $k=>$v){
   echo "<tr><td>{$k}</td><td>{$v}</td></tr>";
   $total += $v;
  }
  echo '<tr><td>total</td><td>'.$total.'</td></tr>';
  echo '</table>';
 }
}
?>
<form action='POST'>
<input type='text' name='from' value='<?php echo date("Y-m-d H:i:s",strtotime('-1 month'));?>'/>
<input type='text' name='to' value='<?php echo date("Y-m-d H:i:s");?>'/>
<input type='submit' name='submit' value='count'/>
</form>

pretty crude but should work, i've not tested so sorry if it has mistakes

Ardav yeah it your code coz its the best solution :D

Also i tested code you posted and it works great but could you say something more about how to get date from those text fields??

Also i dont need to use hours,min and sec. Only Year,moth,day:D

Member Avatar
diafol

Right, the time element is only there because we're using strtotime. There are loads of ways to do this, but anyway:

//You need to check the validity of the data - that it's in yyyy-mm-dd and that it's a valid date - use checkdate()

$datefrom = $_POST['datefrom'];
$dateto = $_POST['dateto'];

//anyway once you're sure you've got valid dates:

$datefrom = $datefrom . " 00:00:00";
$dateto = $dateto . " 23:59:59";
 
$lines = file('Kier.txt');
$list = "";
foreach($lines as $line){
   	$r = explode(",",$line);
   	if(strtotime($r[0]) >= strtotime($datefrom) && strtotime($r[0]) <= strtotime($dateto))$list .= "\n\t<li>{$r[1]}</li>";
}
if($list !="")$list = "\n<ul>$list\n</ul>";

Biiim had the right idea with the form:

<form action="post" action="...the page you want...">
    <label for="datefrom">From:</label>
    <input type="date" id='datefrom' name="datefrom" />
    <label for="dateto">To:</label>
    <input type="date" id='dateto' name="dateto" />
    <input type="submit" name="submit" id="submit" value="Get Entries" />
</form>

The form uses type="date" which is for HTML5 - some browsers won't take it, so you'd be better off hooking up a datepicker, e.g. jqueryui - although there are loads out there.

commented: He`s the man :D +1

When i try it i got those errors:

Notice: Undefined index: datefrom in C:\xampp\htdocs\index.php on line 7
Notice: Undefined index: dateto in C:\xampp\htdocs\index.php on line 8

and after clicking button there is 404 pgae erorr

Member Avatar
diafol

I assume you did this:

if(isset($_POST['submit'])){

//all of the create list php code here 

}

right? otherwise you will get an error as $_POST and $_POST will not be set when you enter the page for the first time.

The 404:

<form action="post" action="...the page you want...">

Did you change action="...the page you want..." to the page url? Does it send to itself? If so, you can delete the action attribute entirely.