Divide 24 hours into 3 shifts schedule with MySQL date_format function

Language
I'm currently developing an application for shifting schedule in our team and I would like to share with you guys on how to divide 24 hours into 3 shifts. The following schedules are: First shift: 7-15 (7am-3pm) Second shift: 15-23 (3pm-11pm) Third shift: 23-7 (11pm-7am) I used date_format function in MySQL and call the current system time, and I also declare variable for each shifts in and out shown below: $first_shift_time_in = 7; $first_shift_time_out = 14; $second_shift_time_in = 15; $second_shift_time_out = 22; $third_shift_time_in_a = 23; $third_shift_time_in_b = 0; $third_shift_time_in_b = 1; $third_shift_time_out_b = 6; Then I used for loop function to iterate the shifts time. Just copy the script below and name it "shiftTimeSched.php" and run to your browser.
  1. <?php
  2.         require ("connection.php");    
  3.         $query = mysql_query("select date_format(now(), '%H') as time") or die (mysql_error());
  4.         while ($row = mysql_fetch_array($query)) {
  5.                 $time = $row[time];
  6.         }
  7.         //echo "<br>";
  8.         $first_shift_time_in = 7;
  9.         $first_shift_time_out = 14;
  10.         $second_shift_time_in = 15;
  11.         $second_shift_time_out = 22;
  12.         $third_shift_time_in_a = 23;
  13.         $third_shift_time_in_b = 0;
  14.         $third_shift_time_in_b = 1;
  15.         $third_shift_time_out_b = 6;
  16.        
  17.         for($i = $first_shift_time_in; $i <= $first_shift_time_out; $i++) {
  18.                 if($time == $i) {
  19.                         $shift = "First Shift";
  20.                 }
  21.         }
  22.         for($ii = $second_shift_time_in; $ii <= $second_shift_time_out; $ii++) {
  23.                 if($time == $ii) {
  24.                         $shift = "Second Shift";
  25.                 }
  26.         }
  27. //      echo "<br>";
  28.         for($iii = $third_shift_time_in_b; $iii <= $third_shift_time_out_b; $iii++) {
  29.                 if($time == $iii) {
  30.                         $shift = "Third Shift";
  31.                 }
  32.         }
  33.         if($time == 23) {
  34.                 $shift = "Third Shift";
  35.         }
  36.         if($time == 0) {
  37.                 $shift = "Third Shift";
  38.         }
  39. echo "You are: ". $shift;      
  40. ?>
Hope this will help you guys, please leave your comment below. Happy codings:)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment