Skip to content

Instantly share code, notes, and snippets.

@billcreswell
Created February 8, 2014 12:15
Show Gist options
  • Save billcreswell/8882783 to your computer and use it in GitHub Desktop.
Save billcreswell/8882783 to your computer and use it in GitHub Desktop.
For a time range, like a time card entry, check that input range of time is valid, and check against a table with times entered to avoid overlap; Allow times to "touch" in a minute range.
/**
* @brief Check for Valid Time In and Out
*
* @params arr(in_time, out_time, technichian_id);
* @return json (success,message);
*/
private function isValidRange($params)
{
// validate input
if ($params["in_time"] == "1969-12-31 19:00") die("{'success': false, 'message': 'Incorrect Time In.'}");
if ($params["out_time"] == "1969-12-31 19:00") die("{'success': false, 'message': 'Incorrect Time Out.'}");
if ($params["in_time"] == $params["out_time"]) die("{'success': false, 'message': 'Out is In.'}");
if ($params["in_time"] > $params["out_time"]) die("{'success': false, 'message': 'Greater In than Out Time.'}");
if (!$this->isOpenRange($params)) die("{'success': false, 'message': 'Time Range Overlap'}");
}
/**
* @brief Check Entry Against Existing Time Periods
*
* @params arr(in_time, out_time, technichian_id);
* @return bool
*/
private function isOpenRange($params)
{
try{
$in_time = new DateTime($params["in_time"]);
$out_time = new DateTime($params["out_time"]);
$in_time->add(new DateInterval('PT1M'));
$out_time->sub(new DateInterval('PT1M'));
// pad with 1 second to allow touching times;
$adjusted_in_time=$in_time->format("Y-m-d H:i");
$adjusted_out_time=$out_time->format("Y-m-d H:i");
$_bind = array(
new Database_BindParam(":technician_id", $params["technician_id"], PDO::PARAM_INT),
new Database_BindParam(":in_time", $adjusted_in_time, PDO::PARAM_STR),
new Database_BindParam(":out_time", $adjusted_out_time, PDO::PARAM_STR),
new Database_BindParam(":in_time2", $adjusted_in_time, PDO::PARAM_STR),
new Database_BindParam(":out_time2", $adjusted_out_time, PDO::PARAM_STR)
);
$SQLisOpenRange = "--
SELECT
a.technician_id,
a.record_id,
a.in_time,
a.out_time
FROM service_time_card a
WHERE (
a.in_time BETWEEN :in_time AND :out_time
OR
a.out_time BETWEEN :in_time2 AND :out_time2
)
AND technician_id = :technician_id";
$rs = $this->db->query($SQLisOpenRange,$_bind);
$result = $this->db->fetch($rs);
$msg = $SQLisOpenRange;
$msg .= ";".$tehcnician_id .":" . $adjusted_in_time .":". $adjusted_out_time;
if($result["technician_id"] == $params["technician_id"]) return false;
else return true;
} catch (Exception $_e){
MelodicException::kill($_e,true,$msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment