Hello I have created an attendance system but client requested to include a new fucntion iin it. Like there if employe comes and time in after the set time its should count late for that day means on a table if it is late so it will show 1 and if on time so it will be zero here i created but it only worked for one day i beleive the next day it's not working.

<?php
$timeout_query = "SELECT * FROM presence where user_id = '".$user_id."' AND p_date = '".$date."'";
$recordset     = mysqli_query($connection, $timeout_query);

while($record = mysqli_fetch_array($recordset)) {
    $lasttimein     = $record['timein'];
    $lasttimeout    = $record['timeout'];
    $clock_in_eve   = $record_row['clock_in_eve'];
    $clock_in_nite  = $record_row['clock_in_nite'];
}
?>

And for the html the time in button it is like this

<form name="mail_sent" action="includes/timein.php" method="POST">
    <input type="submit" value="TIME IN" name="time_in" class="time" />

    <div class="clear"></div>
    <input type="hidden" name="timein" value="<?php echo $time; ?>" />
    <p>
    <?php echo $lasttimein; ?></p>
</form>  

To count lates per day i used this script

<?php

$tardy = 0;

if ($lasttimein >= $clock_in_eve || $lasttimein >= $clock_in_nite) {

    $tardy++;
    echo $tardy;
} else {
    echo $tardy;
}


?>

Please let me know where i am mistaken and what is the mistake I am doing

Thank You

What is the error?

I am sorry I forgot to mention the main thing the tardies (Lates) are not counting up the counter remains zero.

+--------+--------+---------+------------+--------------+---------+
| pre_id | timein | timeout |   p_date   | clock_in_eve | user_id |
|   1    |06:14:04|00:00:00 | 2015-02-28 |   06:03:00   |    3    |

clock_in_eve is default value AS defined.

Thank You

So like today I click on time in and condition checks time in is greater than the last time in the counter becomes 1 of its not the counter comes to default.

Like if let suppose the next its also late the ocunter becomes 1 not 2

Here you go

--
-- Table structure for table `presence`
--

CREATE TABLE IF NOT EXISTS `presence` (
  `pre_id` int(11) NOT NULL AUTO_INCREMENT,
  `timein` time NOT NULL,
  `timeout` time NOT NULL,
  `p_date` date NOT NULL,
  `p_day` varchar(25) NOT NULL,
  `clock_in_eve` time NOT NULL DEFAULT '05:03:00',
  `clock_in_nite` time NOT NULL DEFAULT '09:03:00',
  `user_id` int(25) NOT NULL DEFAULT '0',
  PRIMARY KEY (`pre_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=49 ;

--
-- Dumping data for table `presence`
--

INSERT INTO `presence` (`pre_id`, `timein`, `timeout`, `p_date`, `p_day`, `clock_in_eve`, `clock_in_nite`, `user_id`) VALUES
(40, '06:19:10', '06:49:32', '2015-02-23', 'Monday', '05:03:00', '09:03:00', 3),
(41, '06:27:14', '06:49:32', '2015-02-24', 'Tuesday', '05:03:00', '09:03:00', 3),
(45, '05:40:25', '06:49:32', '2015-02-25', 'Wednesday', '05:03:00', '09:03:00', 3),
(46, '06:40:33', '06:49:32', '2015-02-26', 'Thursday', '05:03:00', '09:03:00', 3),
(47, '05:49:24', '06:49:32', '2015-02-27', 'Friday', '05:03:00', '09:03:00', 3),
(48, '05:26:00', '00:00:00', '2015-03-28', 'Saturday', '05:03:00', '09:03:00', 3);

Run query in phpmyadmin

select date_format(p_date,'%b-%Y'), user_id
, sum( if(str_to_date(concat(p_date,' ',timein),'%Y-%m-%d %H:%i:%s') > str_to_date(concat(p_date,' ',clock_in_eve),'%Y-%m-%d %H:%i:%s') ,1,0)) late_days
     from presence
group by date_format(p_date,'%b-%Y'), user_id having late_days>0
order by date_format(p_date,'%b-%Y'), user_id

Please help me out any one please

Well the thing is that I just want to try to count the lates that has have been taken place in a month I have set a default time in the same table of presence so if the time is grater that the default set value it will be count as late so on the backend I tried to make a code using counter technique but it didn't worked you can see above what I have made but that above mentioned is not counting lates it only count once but the next day if the emplooyee clocks in late it is not counting

No one here to help out

Anyone please help me out i am unable to make a function to count lates for the employees

<?php
$timeout_query = "SELECT * FROM presence where timein >= (your default time in)";
$recordset     = mysqli_query($connection, $timeout_query);
//show records of the late time in's
while($record = mysqli_fetch_array($recordset)) {
    $lasttimein     = $record['timein'];
    $lasttimeout    = $record['timeout'];
    $clock_in_eve   = $record_row['clock_in_eve'];
    $clock_in_nite  = $record_row['clock_in_nite'];
}

The query you provided still did'nt worked the counter is still showing 0

how about this one. ill help you here on out but can you tell me about warnings or other errors that will appear

<?php

$timeout_query = "SELECT * FROM presence where user_id = '".$user_id."' AND p_date = '".$date."'";
$recordset     = mysqli_query($connection, $timeout_query);

while($record = mysqli_fetch_array($recordset)) {
    $lasttimein     = $record['timein'];
    $lasttimeout    = $record['timeout'];
    $clock_in_eve   = $record['clock_in_eve'];
    $clock_in_nite  = $record['clock_in_nite'];
}
?>


<form name="mail_sent" action="includes/timein.php" method="POST">
    <input type="submit" value="TIME IN" name="time_in" class="time" />

    <div class="clear"></div>
    <input type="hidden" name="timein" value="<?php echo $time; ?>" />
    <p>
    <?php echo $lasttimein; ?></p>
</form>  


<?php

$tardy = 0;

if ($lasttimein >= $clock_in_eve || $lasttimein >= $clock_in_nite) {

    $tardy++;
    echo $tardy;
} else {
    echo $tardy;
}


?>

Well I have tried your query and its checking but when next day the condition is false the counter becomes zero the previous value will no remain if on monday time in was greater then clock in time the value becomes 1 and next day when the user time in on right time the counter will show 0

I beleive there should be a loop to be running

yes i also noticed that in the code. but can you show me what you have on your code so i can modify it. do you have a default comparison with the code for example $date = date(Y-m-d); so you can loop throug it

date which is sending in the database is $date = date(y-m-d)

Is there any update did you saw the code

the code provided above solve my problem about counting the late employee's. i dont know y that did not solve yours

Yes it is counting the lates but the counter restets wih the new one i mean it will show only current one the counter will not show 2 if next day employe is late but after 2nd day 3rd employee is late the counter becomes zero as it should not do this the counter have to remain 2 as previous 2 days employee was late

Well you need to add a new data field in your table to count thier lates the code given will not store the data of late employee's

Anyone please help me out i am unable to make a function to count lates for the employees

Wondering what is needed here.
1. how many employee late for particular day?
2. how many days particular employee is being late through particular month/year?
3. did particular employee being late on particular day?

Please do confirm which case is matched so that I can try to help.

case number 2 is matching up though

  1. There should be a 'shift' column in table. Else all employee at the night shift will be always counted as late.
  2. Probably don't limit the select statement to particular day, use the filter using year() or month() instead.
  3. Do the counting in the loop. I saw your code in the thread where you only replace $lasttimein with $record['timein'] in the loop where this made the variable always being replaced by the timein of the last day. And after that the checking will only check for this, so it will always be 0 or 1.

Here is the query to select current year:
SELECT * FROM presence WHERE year(p_date) = year(CURRENT_DATE()) AND user_id = '".$user_id."'
Then

$tardy = 0;
while($record = mysqli_fetch_array($recordset)) {
    if(($record['shift'] == "nite" && $record['timein'] >= $record['clock_in_nite']) || ($record['shift'] == "eve" && $record['timein'] >= $record['clock_in_eve'])){
        $tardy++;
    }
}

Thank you very much for your kind help its working though well but please I have a question in my mind

  1. Shift I have to make set as eve in presence table but when registering user it has to be set there so every time when user timne in i have to send the allowed clock in and shift there as well though as its incrementing but the allowed set will not be sending day by day did you got y point though

    SELECT * FROM presence WHERE year(p_date) = year(CURRENT_DATE()) AND user_id = '".$user_id."'

  2. Can you explain me what you have done here though where you had set a a query that is not fimiliar for me though

Thank You once again just remove the question that have been pulled up in my mind as i beleive i have to place a foreign key and get data from users

First, if the shift is stored in 'User' table, then you might want to select the shift from user instead of adding it to presence table. I done adding it into presence table as because I had the consideration for some job(example: chef) who will having different shift in different days(example night shift in monday+thursday, noon shift in tuesday+friday etc)

Then about the qeury SELECT * FROM presence WHERE year(p_date) = year(CURRENT_DATE()) AND user_id = '".$user_id."'
1. year(p_date): as your database 'p_date' is having the datatype:date, so this will retrieve the year of the p_date(example: p_date = '20150514', then the year(p_date) = '2015' while month(p_date) = '05'
2. CURRENT_DATE() is to get the current date of the server.

efect thank you very much for your kind help thank you once again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.