php barcode scanner automatically log times

Discussion in 'PHP' started by xbat, Sep 16, 2013.

  1. #1
    I am looking to make a page that automatically logs a time when a bar code is scanned. I am looking to do this with php.. Any suggestions would greatly be appreciated.
     
    Solved! View solution.
    xbat, Sep 16, 2013 IP
  2. stelthius

    stelthius Member

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #2
    Cant you just time-stamp the action? and log it.
     
    stelthius, Sep 16, 2013 IP
  3. xxxize

    xxxize Member

    Messages:
    33
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    38
    #3
    The best is to use javascript in form element visible or hid on event onchange get the time stamp.
     
    xxxize, Sep 16, 2013 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Don't overthink it.

    the barcode reader is just a data entry tool - like a keyboard. Open up excel, and scan a barcode - it will just put numbers in a cell.

    So you can
    • have a webpage open
    • cursor in an input field
    • barcode gets scanned
    • numbers entered into the input field
    • change event recognises that the number has been entered. Tests number of characters (for example) and when it looks right it uses ajax (jquery, etc) to submit the form, save the code and refresh the page.
     
    sarahk, Sep 16, 2013 IP
  5. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Fantastic. ok now my only questions are...
    I just need a good idea of doing it a certain way...
    How would I get the total amount from a timestamp along the lines of 2013-09-18 22:10:10 Whats the best way of subtracting and adding?
    How would I display the last two weeks?
     
    xbat, Sep 18, 2013 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #6
    getting the history for a time stamp is as simple as querying the database - and adding/subtracting info is at php.net/date - the easiest way I've found is to convert the timestamp into a "time", add/subtract as needed and convert back into a date.

    If you are using ajax to log the time you can use the same call to return the info and display it in a div somewhere on the page.
     
    sarahk, Sep 18, 2013 IP
  7. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #7
    Ok heres where I am stuck...
    My time doesn't seem to be adding correctly I am doing something stupid but not exactly sure here...

    two things my right column just needs to have the correct amount of time for that row.

    one row equals the correct time however when it goes the next row it calulates the time of when it was punched out and adds it to to that time and then adds to the right column

    example of what it is doing>>
    row1-time-in - row2-time-out =5 minutes + 5 minutes total time
    row1-time-inb - row2-time-outb = 4 minutes + 11 minutes total time

    bottom total time 11 minutes

    what I need it to do >>
    row1-time-in - row2-time-out =5 minutes
    row1-time-inb - row2-time-outb = 4 minutes

    total time 9 minutes
    [​IMG]



    
      mysql_select_db($db_name, $sqlconnect);
    $query_hourgrab_time = "SELECT * FROM hours WHERE employee_id=$user_id AND (in_break_out LIKE '%punch_break_in%' OR in_break_out LIKE'%punch_in%')ORDER BY id ";
    $hourgrab_time = mysql_query($query_hourgrab_time, $sqlconnect) or die(mysql_error());
    $row_hourgrab_time = mysql_fetch_assoc($hourgrab_time);
    $totalRows_hourgrab_time = mysql_num_rows($hourgrab_time);
    
    //grab out and punch out hours for caluation totla
     
        mysql_select_db($db_name, $sqlconnect);
    $query_hourgrabout_time = "SELECT * FROM hours WHERE employee_id=$user_id AND (in_break_out LIKE '%punch_break_out%' OR in_break_out LIKE'%punch_out%') ORDER BY id";
    $hourgrabout_time = mysql_query($query_hourgrabout_time, $sqlconnect) or die(mysql_error());
    $row_hourgrabout_time = mysql_fetch_assoc($hourgrabout_time);
    $totalRows_hourgrabout_time = mysql_num_rows($hourgrabout_time);
       
       
    
          echo '<div class="time_line_clock_amount">';
          do{
        //do{
     
          $date1 = $row_hourgrabout_time['time'];
    
    $date2 = $row_hourgrab_time['time'];
    
    //$hour_total= $date2 - $date1;
    
    //$diff = abs(strtotime($date1) - strtotime($date2));
    
    //$years  = floor($diff / (365*60*60*24));
    //$months  = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    //$days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    
    //$hours  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
    
    //$minuts  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
    
    //$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
    
    // example printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);
    
    ////////////////////THISSSSSSSSSSSWSS
    //$start = strtotime("12/12/2012 10:20:00");
    $start = strtotime($date2);
    //$end = strtotime("12/12/2012 12:59:00");
    $end = strtotime($date1);
    $totaltime = ($end - $start)  ;
    
    $hours = intval($totaltime / 3600);
    $seconds_remain = ($totaltime - ($hours * 3600));
    
    $minutes = intval($seconds_remain / 60);
    $seconds = ($seconds_remain - ($minutes * 60));
    //echo $totaltime;
    echo "$hours:$minutes";
      //  echo":$seconds";
    /// calulation for row only!!!!!
    //$time_toal_row = strtotime($date1 - $date2);
    //echo $time_toal_row;
    //$starttime = $date2;
      //  $stoptime = $date1;
      //
      //
      //
    //echo $diff = $end-$date;
        //$diff = (strtotime($stoptime) - strtotime($starttime));
      // $total = $diff/60;
      //  echo $total = sprintf("%02dh %02dm", floor($total/60), $total%60);
    
    
    
    
    
    //echo $hours; echo $minutes;
    
    //time convert to ftration
    //caluate over 40 hours at the end of the week.
    //ot1
    //vac
    //hol
    
    
    
       
    
    //echo $diff;
    //echo $hour_total;
    echo '<BR>';
    //printf("%d hours, %d minuts\n",  $hours, $minuts ); echo '<BR>';
    
        //  }while ($row_hourgrab_time = mysql_fetch_assoc($hourgrab_time));
     
     
      }while ($row_hourgrabout_time = mysql_fetch_assoc($hourgrabout_time));
    Code (markup):
     

    Attached Files:

    xbat, Sep 19, 2013 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #8
    sarahk, Sep 19, 2013 IP
  9. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #9
    How would I get that to tie into php? I guess that would work long as I can tie it into php. Is that like some sort of a event? Or am I missing something else? I need this for a administrator side. I can't allow access to the mysql server at all, only through php.
     
    xbat, Sep 19, 2013 IP
  10. #10
    I really don't understand your code at all

    walk me through this...
    When someone scans in: a record is saved with the employee_id, time and that they were going in
    When someone scans out: a record is saved with the employee_id, time and that they were going out

    So there are two records for each coming and going... database wouldn't work then, try something like this - it's untested but will give you something to think about
    
    <?php
    mysql_select_db($db_name, $sqlconnect);
    $query_hourgrab_time = "SELECT *, UNIX_TIMESTAMP(`time`) as `unixtime`
      FROM hours
      WHERE employee_id='{$user_id}'
      AND (in_break_out LIKE '%punch_break_in%' OR in_break_out LIKE'%punch_in%' OR in_break_out LIKE '%punch_break_out%' OR in_break_out LIKE '%punch_out%')
      ORDER BY `employee_id`, `time`";
    
    
    // AND (in_break_out LIKE '%punch_break_in%' OR in_break_out LIKE'%punch_in%' OR in_break_out LIKE '%punch_break_out%' OR in_break_out LIKE '%punch_out%')
    // would be better as
    // AND (in_break_out IN ('punch_break_in', 'punch_in', 'punch_break_out', 'punch_out')
    // but it does require the field to hold exact values
    
    
    $hourgrab_time = mysql_query($query_hourgrab_time, $sqlconnect) or die(mysql_error());
    $row_hourgrab_time = mysql_fetch_assoc($hourgrab_time);
    $totalRows_hourgrab_time = mysql_num_rows($hourgrab_time);
    
    //grab out and punch out hours for caluation totla
    
    $raw = array();
    echo '<div class="time_line_clock_amount">';
    while ($row = mysql_fetch_array($row_hourgrab_time)) {
        $cnt = count($row['employee_id']);
        switch ($row['in_break_out']) {
            case 'punch_break_in':
            case 'punch_in':
                $raw[$row['employee_id']][$cnt]['in'] = $row['time'];
                $raw[$row['employee_id']][$cnt]['inunix'] = $row['unixtime'];
                $raw[$row['employee_id']][$cnt]['employee_id'] = $row['employee_id']; // just incase we need to make the result clickable
                break;
            case 'punch_break_out':
            case 'punch_out':
                $cnt = $cnt - 1;
                if (!key_exists('out', $raw[$row['employee_id']][$cnt]))
                    $cnt++;
    
    
    
                $raw[$row['employee_id']][$cnt]['out'] = $row['time'];
                $raw[$row['employee_id']][$cnt]['outunix'] = $row['timeunix'];
    
                $totaltime = ($raw[$row['employee_id']][$cnt]['outunix'] - $raw[$row['employee_id']][$cnt]['inunix']);
                $raw[$row['employee_id']][$cnt]['display'] = date('H:i:s', $totaltime);
        }
    }
    // now make a simpler array that we can sort on time-in
    $output = array();
    foreach ($raw as $employee) {
        foreach ($employee as $row) {
            $output[$row['outunix']] = $row;
        }
    }
    ksort($output);
    ?>
    <table>
        <?php foreach ($output as $row): ?>
            <tr>
                <td><?php echo $row['in']; ?></td>
                <td><?php echo $row['out']; ?></td>
                <td><?php echo $row['display']; ?></td>
            </tr>
        <?php endforeach; ?>
    </table>
    PHP:
     
    sarahk, Sep 19, 2013 IP
  11. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #11
    I'm not doing something correct with this... I was able to change a few items but still getting the same incorrect loop where its not displaying the data for that row. It adds to the next row and so on...

    Ok im getting closer. Now I'm getting some double lines but at least its putting out a different difference in each row.

    time-clock2.jpg

      mysql_select_db($db_name, $sqlconnect);
    $query_hourgrab_time = "SELECT * FROM hours WHERE employee_id=$user_id AND
        (in_break_out LIKE '%punch_break_in%' OR in_break_out LIKE'%punch_in%' OR in_break_out LIKE '%punch_break_out%' OR in_break_out LIKE '%punch_out%')ORDER BY id ";
    $hourgrab_time = mysql_query($query_hourgrab_time, $sqlconnect) or die(mysql_error());
    $row_hourgrab_time = mysql_fetch_assoc($hourgrab_time);
    $totalRows_hourgrab_time = mysql_num_rows($hourgrab_time);
    
    //grab out and punch out hours for caluation totla
    
    //  mysql_select_db($db_name, $sqlconnect);
    //$query_hourgrabout_time = "SELECT * FROM hours WHERE employee_id=$user_id AND (in_break_out LIKE '%punch_break_out%' OR in_break_out LIKE'%punch_out%') ORDER BY id";
    //$hourgrabout_time = mysql_query($query_hourgrabout_time, $sqlconnect) or die(mysql_error());
    //$row_hourgrabout_time = mysql_fetch_assoc($hourgrabout_time);
    //$totalRows_hourgrabout_time = mysql_num_rows($hourgrabout_time);
     
     
    
          echo '<div class="time_line_clock_amount">';
    
     
          do{
          switch ($row_hourgrab_time['in_break_out']) {
            case 'punch_break_in':
            case 'punch_in':
            $time_in_push= $row_hourgrab_time['time'];
           
           
          }
     
     
     
          switch ($row_hourgrab_time['in_break_out']) {
            case 'punch_break_out':
            case 'punch_out':
          $time_out_push= $row_hourgrab_time['time'];
           
           
          }
    
     
    
    
    
            $start = strtotime($time_in_push);
    //$end = strtotime("12/12/2012 12:59:00");
    $end = strtotime($time_out_push);
    $totaltime = ($end - $start)  ;
    
    $hours = intval($totaltime / 3600);
    $seconds_remain = ($totaltime - ($hours * 3600));
    
    $minutes = intval($seconds_remain / 60);
    $seconds = ($seconds_remain - ($minutes * 60));
    
        echo $hours;    echo ' minutes '; echo $minutes;
      //echo $totaltime;
        //echo $total_time_push;
              echo'<BR>';
     
     
     
            }while ($row_hourgrab_time = mysql_fetch_assoc($hourgrab_time));
     
     
          
    PHP:
     
    Last edited: Sep 20, 2013
    xbat, Sep 20, 2013 IP