/index.php

https://bitbucket.org/ben-hudson/wci-schedule · PHP · 159 lines · 146 code · 13 blank · 0 comment · 52 complexity · 8d082a8def61b32ee0a8d03fefb29be5 MD5 · raw file

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="shortcut icon" href="/favicon.png" type="image/png">
  5. <link rel="shortcut icon" type="image/png" href="http://www.wcischeduleapp.com/app/favicon.png" />
  6. <title>WCI Schedule</title>
  7. <script type="text/javascript">
  8. setTimeout(function() {location = 'http://wcischeduleapp.com/app/index.php'}, 60000)
  9. </script>
  10. </head>
  11. <body style="font-family: sans-serif; text-align:center">
  12. <img style="display:block; margin-left:auto; margin-right:auto; width:12%; height:12%" src="http://wcischeduleapp.com/app/icon.png"></img>
  13. <?php
  14. function format_time($minutes)
  15. {
  16. if ($minutes >= 60) {
  17. $hours = floor($minutes / 60);
  18. $minutes -= $hours * 60;
  19. if ($hours > 1) {
  20. if ($minutes == 0) {
  21. return $hours . " hours";
  22. } else if ($minutes == 1) {
  23. return $hours . " hours and 1 minute";
  24. }
  25. return $hours . " hours and " . $minutes . " minutes";
  26. } else {
  27. if ($minutes == 0) {
  28. return "1 hour";
  29. } else if ($minutes == 1) {
  30. return "1 hour and 1 minute";
  31. }
  32. return "1 hour and " . $minutes . " minutes";
  33. }
  34. } else {
  35. if ($minutes > 1) {
  36. return $minutes . " minutes";
  37. }
  38. return "1 minute";
  39. }
  40. }
  41. $link = mysqli_connect("servername", "user", "password", "database");
  42. $success = true;
  43. if (mysqli_connect_errno()) {
  44. $success = false;
  45. }
  46. if ($result = mysqli_query($link, "SELECT * FROM header ORDER BY id LIMIT 1")) {
  47. while ($row = mysqli_fetch_row($result)) {
  48. $starts = strtotime($row[3]);
  49. $ends = strtotime($row[4]);
  50. }
  51. mysqli_free_result($result);
  52. } else {
  53. $success = false;
  54. }
  55. $today = time();
  56. $days = floor(($today - $starts) / (60 * 60 * 24));
  57. $weeks = floor($days / 7);
  58. $school_day = $days - $weeks * 2;
  59. if ($result = mysqli_query($link, "SELECT * FROM holidays")) {
  60. while ($row = mysqli_fetch_row($result)) {
  61. $type = $row[1];
  62. $count = (bool) $row[2];
  63. $date = strtotime($row[3]);
  64. if ($today < $date) {
  65. break;
  66. }
  67. if ($today >= $date && $today < $date + 24 * 60 * 60) {
  68. $holiday = true;
  69. break;
  70. }
  71. if ($today > $date) {
  72. $school_day--;
  73. }
  74. }
  75. mysqli_free_result($result);
  76. } else {
  77. $success = false;
  78. }
  79. $day_of_week = date("w", $today);
  80. if ($day_of_week == 0) {
  81. $school_day--;
  82. } else if ($day_of_week == 1) {
  83. $school_day -= 2;
  84. }
  85. $classes = array(
  86. array("A", "B", "E", "F", "I", "J"),
  87. array("C", "D", "E", "F", "G", "H"),
  88. array("A", "B", "C", "H", "I", "J"),
  89. array("A", "D", "E", "F", "J", "G"),
  90. array("B", "D", "C", "H", "J", "I")
  91. );
  92. if ($success) {
  93. if ($today > $starts && $today < $ends && !$holiday && $day_of_week != 6 && $day_of_week != 0) {
  94. echo date("l F jS g:i A", $today) . " (Day " . ($school_day % 5 + 1) . ") <br /><br />";
  95. $minutes = date("i", $today) + date("G", $today) * 60;
  96. $next_period = 8 * 60 + 14;
  97. if ($minutes < $next_period) {
  98. $minutes_left = $next_period - $minutes;
  99. echo format_time($minutes_left) . " left before the beginning of period " . $classes[$school_day % 5][0];
  100. } else if ($minutes > 14 * 60 + 40) {
  101. echo "School is over for today<br />Next day is Day " . (($school_day + 1) % 5 + 1) . ", first period is period " . $classes[($school_day + 1) % 5][0];
  102. } else {
  103. for ($i = 0; $i < 6; $i++) {
  104. $next_period += 61;
  105. if ($minutes < $next_period) {
  106. $minutes_left = $next_period - $minutes;
  107. if ($i == 5) {
  108. echo format_time($minutes_left) . " left in period " . $classes[$school_day % 5][$i] . "<br />Next day is Day " . (($school_day + 1) % 5 + 1) . ", first period is period " . $classes[($school_day + 1) % 5][0];
  109. } else {
  110. echo format_time($minutes_left) . " left in period " . $classes[$school_day % 5][$i] . "<br />Next period is period " . $classes[$school_day % 5][$i + 1];
  111. }
  112. break;
  113. }
  114. $next_period += 4;
  115. if ($minutes < $next_period) {
  116. $minutes_left = $next_period - $minutes;
  117. echo format_time($minutes_left) . " left before the beginning of period " . $classes[$school_day % 5][$i + 1];
  118. break;
  119. }
  120. }
  121. }
  122. } else {
  123. echo date("l F jS g:i A", $today) . "<br /><br />";
  124. if ($today < $starts) {
  125. echo "The school year hasn't started yet&hellip;";
  126. } else if ($today > $ends) {
  127. echo "The school year is over&hellip;";
  128. } else if ($day_of_week == 6 || $day_of_week == 0) {
  129. echo "No normal school today<br />Next day is Day " . ($school_day % 5 + 1) . ", first period is period " . $classes[$school_day % 5][0];
  130. } else if ($holiday) {
  131. echo "No normal school today (" . $type . ")<br />Next day is Day " . ($school_day % 5 + 1) . ", first period is period " . $classes[$school_day % 5][0];
  132. } else {
  133. echo "Sorry! It looks like something went wrong&hellip;";
  134. }
  135. }
  136. } else {
  137. echo "Sorry! It looks like something went wrong&hellip;";
  138. }
  139. mysqli_close($link);
  140. ?>
  141. <br />
  142. <br />
  143. <div style="font-size:xx-small">The WCI Schedule app is open source! You can check out its source code <a href="https://bitbucket.org/ben-hudson/wci-schedule/overview">here</a>.</div>
  144. </body>
  145. </html>