SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    difference between timestamps

    I am working on a little project that I hope to use at work on just a local server.
    We operate a golf dome and charge by the amount of time people are in the dome hitting golf balls. I want people to be able to enter their name and a pin that clocks the time that they enter the dome to a database. When they enter they name and pin again the time they exit the dome. Then display the difference in hours and minutes only (people wont hit for a day +). and if possible when they clock out I want it to remove the name from the database.
    My database is set up like this:
    id-INT(11),nn,ai
    name-VARCHAR(45),pk,nn
    pin-VARCHAR(4),nn
    time_in-TIMESTAMP,nn
    time_out-TIMESTAMP,nn
    This is what I have so far.
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

    <meta name="description" content="" />

    <meta name="keywords" content="" />

    <meta name="author" content="" />

    <link rel="stylesheet" type="text/css" href="style.css" media="screen" />

    <title>Dome time clock</title>



    </head>

        <body>

            <div id="wrapper">



    <div id="content">





            <div align="center"><h2><small>time clock</small></h2></div><br>
        
        
        
            <table width="100%">
            <tr>
                <td><?php 
     
    // Connects to your Database 

     
    mysql_connect("localhost""root""") or die(mysql_error()); 

     
    mysql_select_db("timelog") or die(mysql_error()); 


     
    //This code runs if the form has been submitted

     
    if (isset($_POST['submit'])) { 



     
    //This makes sure they did not leave any fields blank

     
    if (!$_POST['name'] | !$_POST['pin'] | !$_POST['pin2'] ) {

             die(
    'You did not complete all of the required fields. <a href="javascript:history.go(-1)">Go Back</a>');

         }



     
    // checks if the username is in use

         
    if (!get_magic_quotes_gpc()) {

             
    $_POST['name'] = addslashes($_POST['name']);

         }

     
    $usercheck $_POST['name'];

     
    $check mysql_query("SELECT name FROM log WHERE name = '$usercheck'"

    or die(
    mysql_error());

     
    $check2 mysql_num_rows($check);



     
    //if the name exists it gives an error

     
    if ($check2 != 0) {

             die(
    'Sorry, "'.$_POST['name'].'" is already in use. <a href="javascript:history.go(-1)">Go Back</a>');

                     }


     
    // this makes sure both passwords entered match

         
    if ($_POST['pin'] != $_POST['pin2']) {

             die(
    'Your PIN did not match. <a href="javascript:history.go(-1)">Go Back</a> ');

         }



         
    // here we encrypt the password and add slashes if needed

         
    $_POST['pin'] = md5($_POST['pin']);

         if (!
    get_magic_quotes_gpc()) {

             
    $_POST['pin'] = addslashes($_POST['pin']);

             
    $_POST['name'] = addslashes($_POST['name']);

                 }



     
    // now we insert it into the database

         
    $insert "INSERT INTO log (name, pin)

                 VALUES ('"
    .$_POST['name']."', '".$_POST['pin']."')";

         
    $add_member mysql_query($insert);

         
    ?>



     
     <h1>Thank's <?php echo $_POST['name'?> you are now clocked inn. Enjoy! <a href="javascript:history.go(-1)">Go Back</a></h1>

     <p></p>
     
     <?php 
     


     else 
     {    
     
    ?>


     
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

     <table border="0">

     <tr><td>Username:</td><td>

     <input type="text" name="name" maxlength="60">

     </td></tr>

     <tr><td>Password:</td><td>

     <input type="password" name="pin" maxlength="10">

     </td></tr>

     <tr><td>Confirm Password:</td><td>

     <input type="password" name="pin2" maxlength="10">

     </td></tr>

     <tr><th colspan=2><input type="submit" name="submit" 
    value="Clock in/out"></th></tr> </table>

     </form>


     <?php

     
    }
     
    ?> 
                </td>
            
            </tr>
    </table></p>

    </div> <!-- end #content -->


        </body>

    </html>

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    sounds like you want to use the TIMESTAMPDIFF function
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    sounds like you want to use the TIMESTAMPDIFF function
    That's what I was thinking. Am I going to have to make a form for the clock out part as well? Or can I use the same form and make a different query to send the timestamp to the time_out column? When I try to enter the name and pin again I comes up with my error that the name already in use.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by dxm31 View Post
    Am I going to have to make a form for the clock out part as well? Or can I use the same form and make a different query to send the timestamp to the time_out column?
    most likely the latter

    but the proof, as they say, is in the coding
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can I get the difference in time in hours and minutes using something like this. I want the start time to be pulled from the db and the current time to be logged when submitting the form. Also if you guys dont mind showing me how to make an error when name and pin do not match the row in the db.
    PHP Code:
     if (isset($_POST['submit'])) { 



     
    //This makes sure they did not leave any fields blank

     
    if (!$_POST['name'] | !$_POST['pin']  ) {

             die(
    'You did not complete all of the required fields. <a href="javascript:history.go(-1)">Go Back</a>');

         }


    date_default_timezone_set ("America/Denver");
    $time date"Y-m-d : H:i:s"time());
    $name $_POST["name"];
    $pin $_POST["pin"];

     
    // now checks database for match

         
    $result mysql_query("SELECT * FROM log
    WHERE name LIKE '
    $name' and pin LIKE '$pin'");
     

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    "Timecard for: <b>" $row['name'] . "</b>";
      echo 
    "<br />";    
      echo 
    "Clocked in at: " $row['time_in'];
      echo 
    "<br />";
      echo 
    "Current time: $time";
      echo 
    "<br />"
      
      
    $dateDiff    = ['time_in'] - $time;
       
    $fullHours   floor(($dateDiff-($fullDays*60*60*24))/(60*60));
       
    $fullMinutes floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
       echo 
    $fullHours hours and $fullMinutes minutes.";
      
      }
    ?> 


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
  •