+ Reply to Thread
Results 1 to 5 of 5

Thread: Clock In/Out System

  1. #1
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Clock In/Out System

    Clock In/Out System

    Well I started working hourly recently. I found out that I am horrible about keeping track of my hours. I also wanted to know if my quotes are accurate. I figured the best way to solve both of these problems it to make me a system that tracks my times. This is the tutorial showing you how to do this yourself! You can track your hours, and since you also track hours for non hourly projects you can start giving way more accurate quotes.

    Table:
    id, client(varchar(100)), punch(datetime), comment(longtext)

    Inserting:
    Well inserting should be the first concern you have, since you cannot do math on times you cannot work with. Here is my HTML form:
    Code:
    <?PHP
    $query 
    mysql_query("SELECT client FROM `timecards` GROUP BY `client`");
    while(
    $row=mysql_fetch_assoc($query)) {
        
    $cc .= '<option value="'.$row['client'].'">'.$row['client'].'</option>';
    }
    ?>
        <h3>Add Punch</h3>
        <form method="post">
    <pre>
         Current Client: <select name="client"><?=$cc?><option value="0">New</option></select>
             New Client: <input type="text" name="clientz">
            Comment: <textarea name="comment"></textarea>
                     <input type="submit" value="Punch Card">
    </pre>
         </form>
    The query simply gets all my previous clients and puts them into the select box. How all this really needs to do is to put the contents in the database. Because I believe I am the only one with access to my page I do not have very much filtering on it at all.
    Code:
    if($_POST) {
        
    $now date('Y-m-d H:i:s');
        
    $client preg_replace('/[^a-zA-Z0-9 ]+/''', ($_POST['client']=="0")?$_POST['clientz']:$_POST['client']);
        
    $cmt base64_encode($_POST['comment']);
        
    mysql_query("INSERT INTO `timecards` SET `client`='$client', `punch`='$now', `comment`='$cmt'");

    Basically if you have post data, generate the time in YYYY-MM-DD HH:MM:SS format, I did do a small filter on the client name and then did an if statement to see which client name I should get, the one from the dropdown or the one from the input box, based on which one is longer, I encode the comemnt to make it easier no me, then I insert it into the database.
    Now for the fun stuff!

    Reviewing:
    So with reviewing your timecard you need a way to input two times, the start and end times, get them from the database, display them, display how long each set took, then add up a total at the bottom. So here is my HTML for that. It uses the same $cc variable to get the client list so just make sure your query is closer to the top of the page (before both calls)
    Code:
        <h3>Search</h3>
        <form method="get">
        <pre>
        Client: <select name="client"><?=$cc?></select>
         Start: <input type="text" name="s" value="Last Week">
           End: <input type="text" name="e" value="Now">
                <input type="submit" value="Search Now!">
        </pre>
        </form>
    So converting a string to a time should not be too hard so here is a query for that:
    Code:
    if($_GET) {
        
    $start date('Y-m-d H:i:s'strtotime($_GET['s']));
        
    $end date('Y-m-d H:i:s'strtotime($_GET['e']));
        
    $client preg_replace('/[^a-zA-Z0-9 ]+/'''$_GET['client']);
        
    $query mysql_query("SELECT * FROM `timecards` WHERE `client`='$client' && `punch` BETWEEN '$start' AND '$end' ORDER BY `punch`");

    I have a temporary variable I am using to tell if I am the first or second time. If this is null I am first, if it is not I am not first. My formatting code is here:
    Code:
        while($row=mysql_fetch_assoc($query)) {
            if(
    $t===null) {
                
    $t $row['punch'];
                
    $results .= "<br />".format($row['punch']);
            } else {
                
    $results .= "to ".format($row['punch'])." = ".timediff($t$row['punch']);
                
    $t null;
            }
        }
        if(
    $t !== null) {
            
    $results .= " STILL CLOCKED IN";
        }
        
    $results .= "<br /><br /><strong>Total:</strong> ".His($total); 
    I used a format function which is just what I used to make it look hardly any nicer.
    Code:
    function format($date) {
        return 
    date('M jS g:i:sA'strtotime($date));

    I won't cover it but it makes it look like: Aug 30th 1:43:01AM
    The function that tells me the time difference gets the start and end time and puts it into seconds, subtracts them, and gets the time based on that. The His I named since the date('His') will hopefully format it the same way
    Code:
    function timediff($start,$end$type=0) {
        
    $start strtotime($start);
        
    $end strtotime($end);
        if(
    $start!==-&& $end!==-1) {
            if(
    $end >= $start) {
                return((
    $type==0)?His($end $start):($end $start));
            }
        }
        return 
    false;
    }
    function 
    His($diff) {
        if(
    $hours=floor($diff/3600)) {
            
    $diff -= ($hours*3600);
        }
        if(
    $minutes=floor($diff/60)) {
            
    $diff -= ($minutes*60);
        }
        return 
    "{$hours}:{$minutes}:{$diff}";
    }
    function 
    question($cmt) {
        return 
    " <a href='javascript:alert(\"{$cmt}\")'>?</a> ";

    Here is the full code:
    Code:
    <?PHP
    function His($diff) {
        if(
    $hours=floor($diff/3600)) {
            
    $diff -= ($hours*3600);
        }
        if(
    $minutes=floor($diff/60)) {
            
    $diff -= ($minutes*60);
        }
        return 
    "{$hours}:{$minutes}:{$diff}";
    }
    function 
    question($cmt) {
        return 
    " <a href='javascript:alert(\"{$cmt}\")'>?</a> ";
    }
    function 
    format($date) {
        return 
    date('M jS g:i:sA'strtotime($date));
    }
    function 
    timediff($start,$end) {
        
    $start strtotime($start);
        
    $end strtotime($end);
        if(
    $start!==-&& $end!==-1) {
            if(
    $end >= $start) {
                echo 
    "<br /><br />Start: $end - $start = ".($end-$start)."<br /><br />";
                return 
    $end $start;
            }
        }
        return 
    false;
    }
    if(
    $_POST) {
        
    $now date('Y-m-d H:i:s');
        
    $client preg_replace('/[^a-zA-Z0-9 ]+/''', ($_POST['client']=="0")?$_POST['clientz']:$_POST['client']);
        
    $cmt base64_encode($_POST['comment']);
        
    mysql_query("INSERT INTO `timecards` SET `client`='$client', `punch`='$now', `comment`='$cmt'");
    }
    if(
    $_GET) {
        
    $start date('Y-m-d H:i:s'strtotime($_GET['s']));
        
    $end date('Y-m-d H:i:s'strtotime($_GET['e']));
        
    $client preg_replace('/[^a-zA-Z0-9 ]+/'''$_GET['client']);
        
    $query mysql_query("SELECT * FROM `timecards` WHERE `client`='$client' && `punch` BETWEEN '$start' AND '$end' ORDER BY `punch`");
        while(
    $row=mysql_fetch_assoc($query)) {
            
    $q question(base64_decode($row['comment']));
            if(
    $t===null) {
                
    $t $row['punch'];
                
    $results .= "<br />".format($row['punch']).$q;
            } else {
                
    $j timediff($t$row['punch']);
                
    $results .= "to ".format($row['punch']).$q." = ".His($j);
                
    $total += $j;
                
    $t null;
            }
        }
        if(
    $t !== null) {
            
    $results .= " STILL CLOCKED IN";
        }
        
    $results .= "<br /><br /><strong>Total:</strong> ".His($total);
    }
    $query mysql_query("SELECT client FROM `timecards` GROUP BY `client`");
    while(
    $row=mysql_fetch_assoc($query)) {
        
    $cc .= '<option value="'.$row['client'].'">'.$row['client'].'</option>';
    }
    ?>
    <div style="width:400px;float:left">
        <h3>Search</h3>
        <form method="get">
        <pre>
        Client: <select name="client"><?=$cc?></select>
         Start: <input type="text" name="s" value="Last Week">
           End: <input type="text" name="e" value="Now">
                <input type="submit" value="Search Now!">
        </pre>
        </form>
        <?=$results?>
    </div><div style="width:400px;float:left;">
        <h3>Add Punch</h3>
        <form method="post">
    <pre>
         Current Client: <select name="client"><?=$cc?><option value="0">New</option></select>
             New Client: <input type="text" name="clientz">
            Comment: <textarea name="comment"></textarea>
                     <input type="submit" value="Punch Card">
    </pre>
         </form>
    </div>

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Clock In/Out System

    Very nice work, Blaine! +rep

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Clock In/Out System

    Thank you, Jordan!

  5. #4
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: Clock In/Out System

    Excellent job! +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    princess2313 is offline Newbie
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Re: Clock In/Out System

    Great Work! How exactly did you get the mysql database working?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. countdown clock
    By Orjan in forum JavaScript and CSS
    Replies: 4
    Last Post: 04-20-2010, 05:06 PM
  2. Code: LCD Clock
    By TcM in forum Visual Basic Tutorials
    Replies: 25
    Last Post: 08-03-2009, 09:36 AM
  3. Creating a Clock
    By AfTriX in forum Visual Basic Tutorials
    Replies: 1
    Last Post: 05-08-2009, 08:33 AM
  4. Clock
    By Shaddix in forum Java Help
    Replies: 3
    Last Post: 04-26-2009, 07:19 AM
  5. Cool LCD Clock
    By AfTriX in forum JavaScript Tutorials
    Replies: 2
    Last Post: 01-03-2007, 11:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts