Find working hour for day and night shift in php

2.7k views Asked by At

I am doing attendance system by using php/mysql. Here I got the logical problem when calculate the night shift working hours and If the out time is none.

if(isset($_POST['date']))
{
  $date = $_POST['date'];
}
else
{
  $date = date('Y-m-d');
}
$select = mysql_query("select * from `timelog` where `date`='$date' ORDER BY `time`");
$i=1;
while($row = mysql_fetch_array($select))
{
  if($i==1)
  {
    $fv=$row['time'];
    $first_value=new DateTime($row['time']);
    $k=0;
  }
  if($i==2)
  {
    $sv=$row['time'];
    $second_value=new DateTime($row['time']);
    $difference = $first_value->diff($second_value);
    $total = $difference->format('%h hours %i minutes %s seconds');
    ?>
    <tr><td><?php echo $row['name'];?></td>
    <td><?php echo $row['cardno'];?></td>
    <td><?php echo $row['date'];?></td>
    <td><?php echo date("g:i:s a", strtotime("$fv"));?></td>
    <td><?php echo date("g:i:s a", strtotime("$sv"));?></td>
    <td><?php echo $total;?></td></tr>
    <?php
    $k=1;
  }  
  $i++;
  if($k==1)
 {
  $i=1;
 }
}

In database table I have added id,name,date,time.

1

There are 1 answers

1
user3438804 On BEST ANSWER
if (date("His")<='065959') { $shift_id="N"; }
if (date("His")>='070000' AND date("His")    <='145959') { $shift_id="M"; }
if (date("His")>='150000' AND date("His")<='225959') { $shift_id="E"; }
if (date("His")>='230000') { $shift_id="N"; } print $shift_id;