Skip to content

Instantly share code, notes, and snippets.

@desmayer
Last active July 17, 2017 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desmayer/6e3aa83ebd6507c0688f2da4a0b37996 to your computer and use it in GitHub Desktop.
Save desmayer/6e3aa83ebd6507c0688f2da4a0b37996 to your computer and use it in GitHub Desktop.
Clock In Function for Same/Next Day
//Part of the system allows an admin to change the clock in/out times if any errors happen
//Once updated it needs to work out the difference between the in and out for other use.
$end = strtotime($details[1]);
$end_mid = strtotime('23:59:59');
//dates & times of in & out - assume the same day for both
$comDTin = date('Y-m-d H:i:s', strtotime("$date $time_in"));
$comDTin_raw = strtotime($comDTin);
$comDTout = date('Y-m-d H:i:s', strtotime("$date $end_raw"));
$comDTout_raw = strtotime($comDTout);
//the out time can NEVER be less than the in time. If it is less that means the user clocked out the following day!
if ($comDTout_raw < $comDTin_raw) {
//Next Day Clock Out
$new_date = date('Y-m-d',strtotime($date . "+1 day"));
}else{
//Same Day Clock Out
$new_date = $details[3];
}
if ($date==$new_date){ //Same Day
$dif = $end-$start;
$difmin = round($dif/60);
}else{ //Next Day
$midnight = strtotime('00:01:00');
$dif = $end_mid-$start;
$dif2 = $end-$midnight;
$difmin = round(($dif+$dif2)/60);
}
//This one presented a real challange at first because working with dates & times is a nightmare.
//Get Last clockin where there is not clock out value
$timecheck = mysql_query("SELECT * FROM timekeep WHERE user = '$user' and timediff IS NULL order by date DESC limit 1");
$details = mysql_fetch_row($timecheck);
$date_in = $details[2]; //Clock In Date
$start = strtotime($details[3]); //Clock In Time
$end = strtotime($time); //Current TIme
if ($date_in==$date){ //Same day clock out $date = current date
$dif = $end-$start;
$difmin = round($dif/60);
}else{ //Next day clock out
$end_mid = strtotime('23:59:59'); //End of Day Time
$midnight = strtotime('00:01:00'); //Start of Day Time
$dif = $end_mid-$start; //First difference between Clock In & Midnight
$dif2 = $end-$midnight; //Second difference between Clock Out & 1 min past midnight
$difmin = round(($dif+$dif2)/60);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment