Query between timestamp hours

How can i query data in a table that has a timestamp based on it being between 0 and 16 hours ago, i dont want to pull anything older than 16 hours.

Table structure
1 cidPrimary int(4) No None AUTO_INCREMENT
2 tm_id varchar(12) latin1_swedish_ci No None
3 tm_name varchar(255) latin1_swedish_ci No None
4 hw_id varchar(10) latin1_swedish_ci No None
5 hw_type varchar(20) latin1_swedish_ci No None
6 rs_id varchar(12) latin1_swedish_ci No None
7 job_id int(1) No None
8 lkr_id int(3) No None
9 lkr_com int(4) No None
10 chk_out timestamp Yes NULL
11 chk_in timestamp Yes NULL

here is my current query but its not working correctly

[php]$sql = “UPDATE checkinout SET chk_in = CURRENT_TIMESTAMP WHERE YEAR(chk_out) = YEAR(NOW()) AND MONTH(chk_out) = MONTH(NOW()) AND chk_out >= DATE_SUB(NOW(), INTERVAL 0 HOUR) AND chk_out <= DATE_SUB(NOW(), INTERVAL 16 HOUR) AND tm_id=’” . $tm_id . “’ AND hw_id = '” . $hw_id . “’”;
[/php]
i only want to update the record if the date is within the last 16 hours.

sorry for the poorly written question i am suffering from a severe flu at the moment.

You are close, but don’t need to query the year and month, not that it would work like you have it.

In your where clause you are checking for the chk_out time to be greater than now and less than equal to now - 16 hours ago. See how that would be a problem?

Utilizing the DATE_SUB function:
[php]SELECT DATE_SUB(NOW(), INTERVAL 16 HOUR) as sixteen_hrs_ago[/php]

ok i see that, and thats great and all , i think i may be approaching this wrong. as that still didnt work as i expected it to.

here is what i have,
i have a table that stores information on equipment check in and out

heres what i want
when user checks out a unit the row is inserted into this table with a NULL check in date/time
user would check in this equipment at end of shift which updates the only row in the table that has a null checkin time as long as that same row has a check out time within the last 16 hours, thus ignoring any null check in greater than 16 hours ago, however this should also fail and not allow the check in due to equipment never checked in if there is a null check in older than 16 hours ago.

here is what i have :
The Functions
[php]<?php
function get_user($utmid)
{
$mysqli = new mysqli(‘localhost’, ‘root’, ‘***’, ‘lockers2’);
if ($mysqli->connect_errno) {
echo “Sorry, this website is experiencing problems.”;
echo “Error: Failed to make a MySQL connection, here is why: \n”;
echo "Errno: " . $mysqli->connect_errno . “\n”;
echo "Error: " . $mysqli->connect_error . “\n”;
exit;
}
$sql = “SELECT * FROM users WHERE tm_id = '” . $utmid . “’”;
if (!$result = $mysqli->query($sql)) {
echo “Sorry, the website is experiencing problems.”;
echo “Error: Our query failed to execute and here is why: \n”;
echo "Query: " . $sql . “\n”;
echo "Errno: " . $mysqli->errno . “\n”;
echo "Error: " . $mysqli->error . “\n”;
exit();
}
if ($result->num_rows === 0) {
echo “We could not find a match for ID $utmid, sorry about that. Please try again.”;
exit();
}

$user_data = $result->fetch_assoc();
global $ujobid;
global $utmid;
global $utmname;
$ujobid  = $user_data['job_id'];
$utmid   = $user_data['tm_id'];
$utmname = $user_data['tm_name'];
return $ujobid;
return $utmid;
return $utmname;

}

function get_equip($jobid)
{
$mysqli = new mysqli(‘localhost’, ‘root’, ‘***’, ‘lockers2’);
if ($mysqli->connect_errno) {
echo “Sorry, this website is experiencing problems.”;
echo “Error: Failed to make a MySQL connection, here is why: \n”;
echo "Errno: " . $mysqli->connect_errno . “\n”;
echo "Error: " . $mysqli->connect_error . “\n”;
exit;
}
$sql = “SELECT * FROM equipment WHERE job_id = '” . $jobid . “’ AND e_status = 0 ORDER BY rand() LIMIT 1”;
if (!$result = $mysqli->query($sql)) {
echo “Sorry, the website is experiencing problems.”;
echo “Error: Our query failed to execute and here is why: \n”;
echo "Query: " . $sql . “\n”;
echo "Errno: " . $mysqli->errno . “\n”;
echo "Error: " . $mysqli->error . “\n”;
exit();
}
if ($result->num_rows === 0) {
echo “We could not find a match for HW, sorry about that. Please try again.”;
exit();
}

$equip_data = $result->fetch_assoc();
global $ehwid;
global $etype;
global $ersid;
global $estatus;
global $ejobid;
global $elkrid;
global $elkrcom;
if ($equip_data['rs_inc'] == "1"){
	$ersid = $equip_data['rs_id'];
}else{
	$ersid = "0" ;
}
$ehwid   = $equip_data['hw_id'];
$etype   = $equip_data['type'];
$ersid   = $equip_data['rs_id'];
$estatus = $equip_data['e_status'];
$ejobid  = $equip_data['job_id'];
$elkrid  = $equip_data['lkr_id'];
$elkrcom = $equip_data['lkr_com'];
return $ehwid;
return $etype;
return $ersinc;
return $ersid;
return $estatus;
return $ejobid;
return $elkrid;
return $elkrcom;
$mysqli->close();

}

function lookup_chk($tmid)
{
$mysqli = new mysqli(‘localhost’, ‘root’, ‘***’, ‘lockers2’);
if ($mysqli->connect_errno) {
echo “Sorry, this website is experiencing problems.”;
echo “Error: Failed to make a MySQL connection, here is why: \n”;
echo "Errno: " . $mysqli->connect_errno . “\n”;
echo "Error: " . $mysqli->connect_error . “\n”;
exit();
}
$sql = “SELECT * FROM checkinout WHERE tm_id = '” . $tmid . “’ AND chk_in IS NULL”;
if (!$result = $mysqli->query($sql)) {
echo “Sorry, the website is experiencing problems.”;
echo “Error: Our query failed to execute and here is why: \n”;
echo "Query: " . $sql . “\n”;
echo "Errno: " . $mysqli->errno . “\n”;
echo "Error: " . $mysqli->error . “\n”;
exit();
}
$row_cnt = mysqli_num_rows($result);
$check_data = $result->fetch_assoc();
global $ctmid;
global $ctmname;
global $chwid;
global $chwtype;
global $crsid;
global $cjobid;
global $clkrid;
global $clkrcom;
global $chkout;
global $chkin;
global $cflag;
$crsid = $check_data[‘rs_id’];
$ctmid = $check_data[‘tm_id’];
$ctmname = $check_data[‘tm_name’];
$chwid = $check_data[‘hw_id’];
$chwtype = $check_data[‘hw_type’];
$cjobid = $check_data[‘job_id’];
$clkrid = $check_data[‘lkr_id’];
$clkrcom = $check_data[‘lkr_com’];
$chkout = $check_data[‘chk_out’];
$chkin = $check_data[‘chk_in’];
$cflag = $row_cnt;
return $ctmid;
return $ctmname;
return $chwid;
return $chwtype;
return $crsid;
return $cjobid;
return $clkrid;
return $clkrcom;
return $chkout;
return $chkin;
return $cflag;
$mysqli->close();
}

function set_status($estatus,$ehwid)
{
$mysqli = new mysqli(‘localhost’, ‘root’, ‘***’, ‘lockers2’);
if ($mysqli->connect_errno) {
echo “Sorry, this website is experiencing problems.”;
echo “Error: Failed to make a MySQL connection, here is why: \n”;
echo “Errno: " . $mysqli->connect_errno . “\n”;
echo “Error: " . $mysqli->connect_error . “\n”;
exit();
}
$sql = “UPDATE equipment SET e_status = '”.$estatus.”’ WHERE hw_id = '”.$ehwid."’;";
if (!$result = $mysqli->query($sql)) {
echo “Sorry, the website is experiencing problems.”;
echo “Error: Our query failed to execute and here is why: \n”;
echo "Query: " . $sql . “\n”;
echo "Errno: " . $mysqli->errno . “\n”;
echo "Error: " . $mysqli->error . “\n”;
exit();
}
$mysqli->close();
}

function check_out($utmid,$utmname,$ehwid,$etype,$ersid,$ujobid,$elkrid,$elkrcom,$datenow)
{
$mysqli = new mysqli(‘localhost’, ‘root’, ‘****’, ‘lockers2’);
if ($mysqli->connect_errno) {
echo “Sorry, this website is experiencing problems.”;
echo “Error: Failed to make a MySQL connection, here is why: \n”;
echo "Errno: " . $mysqli->connect_errno . “\n”;
echo "Error: " . $mysqli->connect_error . “\n”;
exit();
}
$sql = “INSERT INTO checkinout (tm_id,tm_name,hw_id,hw_type,rs_id,job_id,lkr_id,lkr_com,chk_out)
VALUES (’” . $utmid . “’,’” . $utmname . “’,’” . $ehwid . “’,’” . $etype . “’,’” . $ersid . “’,’” . $ujobid . “’,’” . $elkrid . “’,’” . $elkrcom . “’,’” . $datenow . “’)”;
if (!$result = $mysqli->query($sql)) {
echo “Sorry, the website is experiencing problems.”;
echo “Error: Our query failed to execute and here is why: \n”;
echo "Query: " . $sql . “\n”;
echo "Errno: " . $mysqli->errno . “\n”;
echo "Error: " . $mysqli->error . “\n”;
exit();
}
$mysqli->close();
}

function check_in($tm_id, $hw_id)
{
$mysqli = new mysqli(‘localhost’, ‘root’, ‘****’, ‘lockers2’);
if ($mysqli->connect_errno) {
echo “Sorry, this website is experiencing problems.”;
echo “Error: Failed to make a MySQL connection, here is why: \n”;
echo "Errno: " . $mysqli->connect_errno . “\n”;
echo "Error: " . $mysqli->connect_error . “\n”;
exit();
}
$sql = “UPDATE checkinout SET chk_in = CURRENT_TIMESTAMP WHERE YEAR(chk_out) = YEAR(NOW()) AND MONTH(chk_out) = MONTH(NOW()) AND DAY(chk_out) = DAY(NOW()) AND tm_id=’” . $tm_id . “’ AND hw_id = '” . $hw_id . “’”;
if (!$result = $mysqli->query($sql)) {
echo “Sorry, the website is experiencing problems.”;
echo “Error: Our query failed to execute and here is why: \n”;
echo "Query: " . $sql . “\n”;
echo "Errno: " . $mysqli->errno . “\n”;
echo "Error: " . $mysqli->error . “\n”;
exit();
}
$mysqli->close();
}
?>[/php]

The Page
[php]

Test

Logo

DC29 Lakeland

Locker Information Lookup

Team Member ID:


Check In?
Submit
.
<?php date_default_timezone_set('America/New_York'); global $datenow; $datenow = date("Y-m-d H:i:s"); require('usr_functions.php'); if (isset($_GET['tm_id'])) { $tmid = $_GET['tm_id']; } else { exit("TM ID Must be entered"); } ?>

×


<?php if (isset($_GET['checkin'])) { get_user($tmid); lookup_chk($utmid, $ujobid); set_status(0, $chwid); check_in($ctmid, $chwid); echo "

" . $utmname . "

"; echo "TM ID:" . $utmid . "
"; echo "Job ID:" . $ujobid . "
"; echo "

Please return equipment to:

"; echo "

Locker: " . $clkrid . " - Combo: " . $clkrcom . "

"; } else { get_user($tmid); lookup_chk($utmid); if ($cflag >= "1") { echo "

EQUIPMENT STILL OUT!

"; echo "

" . $utmid . " - " . $utmname . "

"; echo "
"; echo "

Items Out: " . $cflag . "

"; echo "

Date Checked Out: " . $chkout . "

"; echo "

RF ID: " . $chwid . "

"; if (!$crsid == "0") { echo "

RS ID: " . $crsid . "

"; } echo "

Locker: " . $clkrid . " - Combo: " . $clkrcom . "

"; echo "

Please See A Systems Specialist!

"; } else { get_equip($ujobid); echo "

" . $utmid . " - " . $utmname . "

"; echo "
"; echo "

RF ID:" . $ehwid . "

"; if (!$ersid == "0") { echo "

RS ID: " . $ersid . "

"; } echo "

Locker:" . $elkrid . " - Combo:" . $elkrcom . "

"; echo "Inserting"; set_status(1, $ehwid); check_out($utmid, $utmname, $ehwid, $etype, $ersid, $ujobid, $elkrid, $elkrcom, $datenow);
}

}
?>





[/php]

now before anyone yells at me for the poor coding – I KNOW – this is just a proof of concept project and is never going to be connected to a network so i am not worried about injection or error checking at this point, that will come later once the project passes onto the next phase.

So, you want to allow a checkin ONLY if the check out period was 16 hours or less from the check out timestamp?

correct, i was just thinking perhaps a IF statement here would work …
[php]


<?php
if (isset($_GET[‘checkin’])) {
get_user($tmid);
lookup_chk($utmid,$ujobid);
IF($chkout <> NOW() and 16 hours{
set_status(0,$chwid);
check_in($ctmid,$chwid);
echo “

” . $utmname . “

”;
echo “TM ID:” . $utmid . “
”;
echo “Job ID:” . $ujobid . “
”;
echo “

Please return equipment to:

”;
echo "

Locker: " . $clkrid . " - Combo: " . $clkrcom . “

”;
}else{
//check out was more than 16 hours ago
echo “

” . $utmname . “

”;
echo “TM ID:” . $utmid . “
”;
echo “

Equipment still out

”;
echo "

Equipment: " . $chwid . “

”;
}
}else{
get_user($tmid);
lookup_chk($utmid);[/php]

What happens when the checkout is longer than 16 hours?

should just echo a message stating that they have equipment they didnt turn in last shift and need to see a IT guy

Does this grab anything in test data?

SELECT * FROM `checkinout` WHERE chk_out = DATE_SUB(NOW(), INTERVAL 16 HOUR) ORDER BY tm_id, hw_id

empty result set, FYI im running it in phpmyadmin as typed and we should have 1 result as i have a test row of data with chk_out 2018-01-25 19:55:00

Should there be data coming back?

we should have 1 result as i have a test row of data with chk_out 2018-01-25 19:55:00

http://sqlfiddle.com/#!9/2d4a0e/20

Are you using UTC time or a specific timezone?

specific, here is the code im using to set the date,

[php]date_default_timezone_set(‘America/New_York’);
global $datenow;
$datenow = date(“Y-m-d H:i:s”);[/php]

sorry for double post,

i think i have got it working as expected,
i added a If statement to index that sets a var $hours16 to date(‘Y-m-d H:i:s’, strtotime(’-16 hour’)); then compares them with IF($chkout >= $hours16), and i changed the sql query to "UPDATE `checkinout` SET chk_in = CURRENT_TIMESTAMP WHERE chk_in IS NULL AND tm_id='" . $tm_id . "' AND hw_id = '" . $hw_id . "'";

[php]<?php
date_default_timezone_set(‘America/New_York’);
global $datenow;
$datenow = date(“Y-m-d H:i:s”);
global $hours16;
$hours16 = date(‘Y-m-d H:i:s’, strtotime(’-16 hour’));
require(‘usr_functions.php’);
if (isset($_GET[‘tm_id’])) {
$tmid = $_GET[‘tm_id’];
} else {
exit(“TM ID Must be entered”);
}
?>

×




<?php
if (isset($_GET[‘checkin’])) {
get_user($tmid);
lookup_chk($utmid,$ujobid);
IF($chkout >= $hours16){
set_status(0,$chwid);
check_in($ctmid,$chwid);
echo “

” . $utmname . “

”;
echo “TM ID:” . $utmid . “
”;
echo “Job ID:” . $ujobid . “
”;
echo “

Please return equipment to:

”;
echo "

Locker: " . $clkrid . " - Combo: " . $clkrcom . “

”;
}else{
echo "

EQUIPMENT STILL OUT!

";
echo “

” . $utmid . " - " . $utmname . “

”;
echo “
”;
echo “

Items Out: " . $cflag . “

”;
echo “

Date Checked Out: " . $chkout . “

”;
echo “

RF ID: " . $chwid .”

”;
if (!$crsid == “0”){
echo “

RS ID: " . $crsid .”

”;
}
echo "

Locker: " . $clkrid . " - Combo: " . $clkrcom . “

”;
echo "

Please See A Systems Specialist!

";
}[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service