Divide 24 hours into 3 shifts schedule with MySQL date_format function

Submitted by: 
Language: 
Visitors have accessed this post 2497 times.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.