Lissa
Lissa

Reputation: 23

how to Increament date daywise

i have in my database records based on team types.now i need the system to show records of entire team from database based on dates entered i.e if i enter 06-June-2012 to 16-june-2012 it should show me all records from first date to other date. (in php)

$sql="SELECT mstEmp.empname,InDate, dailyattendance.InTime,dailyattendance.OutTime,mstEmp.teamtype FROM mstEmp left join dailyattendance on (dailyattendance.HolderName = mstEmp.empname) where (InDate between '$from' AND '$to') and mstEmp.teamtype='$chk' GROUP BY mstEmp.empname";
$res = mysql_query($sql);

while($row = mysql_fetch_row($res)) {           
  list($name,$indate,$in,$out,$t) = $row;
  if($row[1]==NULL ||$row[2]==NULL || $row[3]==NULL) { 
    $row[3]='NULL' ;
    $row[2]='NULL' ;
  } else {
    $row[2] = date("g:i A", $in);
    $row[3]= date("g:i A" ,$out);
  }
  $d3 = date("d-m-Y (D)", $in);
  $bg = '';
  if(date('D', $in) == 'Sun' || date('D', $in) == 'Sat')
    $bg = 'bgcolor=#EEEEEE';

  echo "<tr $bg><td colspan=4>Date: ".$d3."</td></tr>";
  if($indate != $date) {
    $date = $indate;
    echo "<tr>";
    echo "<td align=left width='35%'>".trim($name)."</td>";
    echo "<td align=center width='20%' >".$row[2]."</td>";
    echo "<td align=center  width='23%' >".$row[3]."</td>";
    echo "<td align=center width='16%'>".$t."</td>";
    //echo "</tr></table><br>";
  }
}
echo "</tr></table><br>";
echo "</form>";

Upvotes: 0

Views: 136

Answers (1)

poncha
poncha

Reputation: 7866

If you have a date (string), and you want to add N days to it:

$date = date($format, strtotime($date, "+{$N}day"));

If you want to calculate date N days from now:

$date = date($format, time() + $N*86400); // 86400 is number of seconds in 1 day

If, on the other hand (and thats what it looks like from your question really...) you're looking to filter results based on a date range, then you have two options:

  1. Filter the results in SQL (before php) - WHERE date BETWEEN '$date1' and '$date2' I would strongly suggest to stick with this option, because if your table is going to have a lot of records, filtering results in php will be cpu and memory consuming.

  2. You can convert both dates to seconds first - $time1 = strtotime($date1) (and same for the second date, and then every time you need to compare to the range, convert the date you need to compare as well, and just check if the resulting integer is between the two you calculated earlier.


If you have two date ranges (one in input - $from and $to) and one in database (InDate and OutDate columns), then to get the records where these ranges cross you can do this:

`WHERE InDate >= '$to' AND OutDate <= '$from'`

If you just want to loop (in php) from $dateA to $dateB, then you can do this:

// strtotime() converts date to number of seconds since Jan 1 1970
// 86400 is number of seconds in a day, so it ensures steps of 1 day
for($time=strtotime($dateA); $time<=strtotime($dateB); $time+=86400) {
}

Upvotes: 1

Related Questions