0

Hello guys I really need your help to save my job. i started to create and attendance system but I stucked up with time in button.

I have created a time out button and working fine and great but the problem is that I am unsuccessfull for creating a time in that when user sign's in and click's on time in button current time shows on the table and inserts into database and the time in button will not work anymore until the next day starts.

Can you guys please help me out for creating this??

here are some codes that i would like to share with you

My home page code for time in and time out :

home.php code :

<?php

date_default_timezone_set("Etc/GMT+7");
include_once("include/uchecksession.php");
$uid = $_SESSION["uid"];            
include_once("include/config.php");

?>
<script language="javascript">

function timeout() {
document.form1.action = "settime.php?uid=<?php echo $uid?>";
document.form1.submit();
}

</script>
<html>
<head>
</head>

<body>
<?php

$result = $_GET["result"];
date_default_timezone_set("Etc/GMT+7");
$date = date('Y-m-d');
$day = date('l');
$min = date('i');
$hour = date('H');

$min = $min - 5;
if($min<0){ $hour=$hour-1; $min=$min+65;}
$time = $hour.":".$min;
$settime = $time;
$settime1 = date('h:i:s A');

?>

<div id="main">
<div id="main-right">

        <div class="clear"></div>
        <form name="form1" method="post" action= "" >
            <div id="time-status">
            <h4>Time Status</h4>
            <?php
                        date_default_timezone_set("Etc/GMT+7");

                        $query = "select * from presence where uid=".$uid." and date='".$date."'";
                        $recordset = mysql_query($query);
                        while($record = mysql_fetch_array($recordset)){
                        $count = 1;
                        //echo "<td>Your Time In :==: ",$record["timein"],"</td></tr><br></table>";
                        echo "<table><tr><td>Your Time In :</td><td> ",$record["timein"],"</td></tr></table>";
                        $lasttimeout = $record["timeout"];
                        }

                        echo "<table><tr><td>Your Time Out :</td><td> ",$lasttimeout,"</td></tr></table>";
            ?>

            <input type="button" name="button2" value="Time Out" onClick="timeout()" class="button">
            </div>

            <div id="log">
            <h4>User Log</h4>
            <table border="1" bordercolor="#CCCCCC">
            <tr> 
                <th scope="col"><div align="center">User Name </div></th>
                <th scope="col">Time Req. </th>
                <th scope="col">Time Spent </th>

            </tr>

            <?php

            date_default_timezone_set("Etc/GMT+7");
                 $result = $_GET["result"];

                $date = date('Y-m-d');
                $day = date('l');
                $min = date('i');
                $hour = date('h');

                $min = $min - 5;
                if($min<0){ $hour=$hour-1; $min=$min+60;}
                $time = $hour.":".$min;
                $settime = $time;
                $settime1 = date('h:i:s A');

                $query = "select * from presence where uid=".$uid." and date='".$date."'";
                $recordset = mysql_query($query);
                while($record = mysql_fetch_array($recordset)){
                $count = 1;
                //echo "<td>Today you timed in at:- ",$record["timein"],"</td></tr><br></table>";
                //$lasttimeout = $record["timeout"];
                }
                if($count!=1) 
                {
                $query2 = "insert into presence(timein, timeout, date, uid, pday) 
                values('$settime', '$settime1', '$date', $uid, '$day')";
                $af = mysql_query($query2);
                //echo "<td>Today you timed in at:- ",$settime,"</td></tr><br></table>";
                $lasttimeout = date('H:i');
                }

                //echo "<table><tr><td>Today you last timed out at:- ",$lasttimeout," 
                //</td></tr></table>";
                 $array1 = array();
                    $array2 = array();
                    $array3 = array();
                    $array4 = array();
                    $userid = array();
                    $great = 0; 
                 function calculate($timein, $timeout) {
                    $timespentin = split(":",$timein,3);
                    $timespentout = split(":",$timeout,3);
                    //print_r($timespentin);
                    //print_r($timespentout);
                    $t1 = $timespentin[0]*60 + $timespentin[1];
                    $t2 = $timespentout[0]*60 + $timespentout[1];
                    $timespent = round(($t2 - $t1)/60,2);
                    //echo $timespent;
                    return $timespent;
                    }

                    $sday = 1;
                    $today = date ("d");
                    $month = date("m");
                    $year = date("Y");
                    $date1 = $year."-".$month."-".$sday;
                    $date2 = $year."-".$month."-".$today;

                    $recordset5 = mysql_query("select * from nwdays where nwdate between '".$date1."' and '".$date2."' and uid=0  and holidaytype=2 order by nwdate");
                    $ndays = 0;
                    while($record5 = mysql_fetch_array($recordset5)) {
                        $ndays = $ndays + 1;
                    }
                    $towday = $today - $ndays;

                    $recordset15 = mysql_query("select * from nwdays where nwdate between '".$date1."' and '".$date2."' and uid=".$uid." and holidaytype=0 order by nwdate");
                    $undays = 0;
                    while($record15 = mysql_fetch_array($recordset15)) {
                        $undays = $undays + 1;
                    }
                    //$towhours = $towday * 8;
                    $towday = $towday - $undays ;

                $query = "select * from users where uid =".$uid;
                $recordset = mysql_query($query);
                while($record = mysql_fetch_array($recordset)) {
                $whours = $record["uwhours"];
                $towhours = $towday * $whours;
                $totaltime = 0;
                $query1 = "select * from presence where uid =".$uid." and date between '".$date1."' and '".$date2."' order by date";
                $recordset1 = mysql_query($query1);
                while($record1 = mysql_fetch_array($recordset1)) {
                $timein = $record1["timein"];
                $timeout = $record1["timeout"];

                $daytime = calculate($timein, $timeout);
                $totaltime = $totaltime + $daytime;
                }
                $ttimehm = explode(".",$totaltime,2);
                $hours = $ttimehm[0];
                $ttimehm = split(".",$totaltime,2);
                $minutes = round ($ttimehm[1]*60,0);
                //echo $hours,"---",$minutes;
                if ($minutes >59) {$minutes = $minutes%60;}
                $div = $minutes/10;
                if($div<1) {$minutes = "0".$minutes;}

                $totaltime = $hours.":".$minutes;
                }

                $month = date("m");
                    $year = date("Y");

                 $startday = 1;
                 $endday = date("d");

                    $date1 = $year."-".$month."-".$startday;
                    $date2 = $year."-".$month."-".$endday;

                    $recordset9 = mysql_query("select * from configuration where setname='relaxedhours'");
                    while($record9 = mysql_fetch_array($recordset9)) {
                    $relaxedhours = $record9["setvalue"];
                    }

                    $recordset4 = mysql_query("select * from nwdays where nwdate 
                    between '".$date1."' and '".$date2."' and uid=0 and holidaytype=2 order by nwdate");

                    $days = 0;
                    while($record4 = mysql_fetch_array($recordset4)) {
                        $days = $days + 1;
                    }
                    $wdays1 = $endday - $days;
                    //$whours = $wdays *8;

                $recordset = mysql_query("select * from users");
                while($record = mysql_fetch_array($recordset)) {

                $recordset15 = mysql_query("select * from nwdays where nwdate between '".$date1."' and '".$date2."' and uid=".$record["uid"]." and holidaytype=0 order by nwdate");
                    $undays = 0;
                    while($record15 = mysql_fetch_array($recordset15)) {
                        $undays = $undays + 1;
                    }
                    $wdays = $wdays1 - $undays;

                $whours1 = $record["uwhours"];
                $whours = $wdays * $whours1;
                $totaltime = 0;
                $query1 = "select * from presence where uid =".$record["uid"]." and date between '".$date1."' and '".$date2."' order by date";
                $recordset1 = mysql_query($query1);
                while($record1 = mysql_fetch_array($recordset1)) {
                $timein = $record1["timein"];
                $timeout = $record1["timeout"];
                $totaltime = $totaltime + calculate($timein, $timeout);
                //$timespent = split(":",$timein,2);
                //print_r($timespent);
                }
                $ttimehm = explode(".",$totaltime,2);
                $hours = $ttimehm[0];
                $ttimehm = split(".",$totaltime,2);
                $minutes = round ($ttimehm[1]*60,0);
                if ($minutes >59) {$minutes = $minutes%60;}
                $div = $minutes/10;
                if($div<1) {$minutes = "0".$minutes;}
                $totaltime = $hours.":".$minutes;
                $userid["$great"] = $record["uid"];     
                    $array1["$great"] = $record["ulogin"];      
                    $array2["$great"] = $whours;
                    $array3["$great"] = $totaltime;
                    //$array4["$great"] = $minutes;
                    $great++;   
                }

                $greatest = 0;
                for ( $i=0; $i < $great; $i++ )
                {
                     $greatest = $array3[$i];
                     $greatest = split(":",$greatest,"2");
                     $array3[$i] = ($greatest[0] * 60) + $greatest[1];
                }        

                for ( $i=0; $i < $great; $i++)
                {
                     for ( $j=$i+1; $j < $great; $j++ )
                     {                         
                           if ( $array3[$j] > $array3[$i])
                           {                    
                                $temp1 = $array1[$j];
                                $temp2 = $array2[$j];
                                $temp3 = $array3[$j];
                                $temp4 = $userid[$j];                   

                                $array1[$j] = $array1[$i];
                                $array2[$j] = $array2[$i];
                                $array3[$j] = $array3[$i];
                                $userid[$j] = $userid[$i];

                                $array1[$i] = $temp1;
                                $array2[$i] = $temp2;
                                $array3[$i] = $temp3;
                                $userid[$i] = $temp4;                   
                           }        
                     }
                }

                for ( $i=0; $i < $great; $i++ )
                {
                      $totaltime = $array3[$i];
                      $totalhours = intval($totaltime/60);     
                      $totalminutes = $totaltime%60;
                      if($totalminutes<10) {$totalminutes = "0".$totalminutes;}
                      $array3[$i] = $totalhours.":".$totalminutes;

                }
                for ( $i=0; $i < $great; $i++ )
                {            
                      if(($array3[$i]+$relaxedhours)< $array2[$i]) 
                      {
                          if ( $uid == $userid["$i"] )
                               echo"<tr bgcolor='skyblue'><td><div align='left'>",$array1["$i"],"</td>
                                        <td align='center'>",$array2[$i],"</td><td align='center'><font color='red'>",$array3[$i],"</font></td></tr>"; 
                          else        
                               echo"<tr><td><div align='left'>",$array1["$i"],"</td>
                                        <td align='center'>",$array2[$i],"</td><td align='center'><font color='red'>",$array3[$i],"</font></td></tr>";
                      }

                      else
                      {
                          if ( $uid == $userid["$i"] )
                               echo"<tr bgcolor='skyblue'><td><div align='left'>",$array1["$i"],"</td>
                                       <td align='center'>",$array2[$i],"</td><td align='center'>",$array3[$i],"</td></tr>";
                          else
                              echo"<tr><td><div align='left'>",$array1["$i"],"</td>
                                       <td align='center'>",$array2[$i],"</td><td align='center'>",$array3[$i],"</td></tr>";
                       }
                }
            ?> 
            </table>
            </div>
            </form>    
    </div>
</div>   
</div>
</div>

Code for time out button :

settime.php - Code :

<?php

$uid = $_GET["uid"];
//if($uid==""){
//header("Location:login.php");
//}
include_once("include/config.php");
//include_once("include\menu.php");
date_default_timezone_set("Etc/GMT+7");
$date = date('Y-m-d');
$settime = date('h:i:s A');
$query = "update presence set timeout = '".$settime."' where uid=".$uid." and date='".$date."'";
$af = mysql_query($query);
if($_GET["admin"]==1) 
{
header("Location:admin.php?uid=".$uid);

}

else
{
header("Location:home.php?uid=".$uid);

}

?>

Above are the codes see and let me know if you guys can help me out I would be really really thankfull to you all.

Thank You

Kind Regards
Usman Khan

2
Contributors
2
Replies
22
Views
3 Years
Discussion Span
Last Post by UK-1991
0

Too much irrelevant code. The timeout code was clear. Please just post the timein code. It should be very much the same as timeout, just with in-time instead of out-time.

Also, PHP is an object-oriented language, very similar to C++. DO NOT mix PHP and HTML/JS. Build up your HTML and JavaScript code strings in your PHP classes as string values, and then output the strings when needed. This provides a couple of major advantages to the mixed domain you are using:

  1. It is easier to debug.
  2. You don't get unintended side-effects.
  3. Changing environment issues are much better dealt with - you can change the strings before they are output.
0

Yes I was thinking that same I created timin as same as you said here is the code :

<?php

$uid = $_GET["uid"];
include_once("include/config.php");

date_default_timezone_set("Etc/GMT+7");
$date = date('Y-m-d');
$settime = date('h:i:s');
$query = "update presence set timein = '".$settime."' where uid=".$uid." and date='".$date."'";
$af = mysql_query($query);

if($min<0){ $hour=$hour-1; $min=$min+65;}
$time = $hour.":".$mink;
$settime = $time;
$settime1 = date('h:i:s A');

if($_GET["admin"]==1){
header("Location:admin.php?uid=".$uid);

} else{
header("Location:home.php?uid=".$uid);

}

?>

On home page at timin area I have filled this code :

<?php
                        date_default_timezone_set("Etc/GMT+7");

                        $query = "select * from presence where uid=".$uid." and date='".$date."'";
                        $recordset = mysql_query($query);
                        while($record = mysql_fetch_array($recordset)){
                        $count = 1;
                        //echo "<td>Your Time In :==: ",$record["timein"],"</td></tr><br></table>";
                        $lasttimein = $record["timein"];
                        echo "<table><tr><td>Your Time In :</td><td> ",$lasttimein,"</td></tr></table>";
                        $lasttimeout = $record["timeout"];
                        }

                        echo "<table><tr><td>Your Time Out :</td><td> ",$lasttimeout,"</td></tr></table>";
            ?>

            <input type="button" name="button2" value="Time Out" onClick="timeout()" class="button">
            <input type="button" name="button3" value="Time In" onClick="timein()" class="button">
            </div>

Working fine but the main thing is it should be clicked once wiithin 24 hours not to be able to click again and again I hope you understand what I am trying to say :)

And please let me know have i done correct ?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.