Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. date_default_timezone_set('Europe/London');
  2. class RestartTimer
  3. {
  4.  
  5. /**
  6. * $restartTime used to store restart times of server
  7. * @var array
  8. */
  9. private $restartTimes = array("02:00","06:00","10:00","14:00","18:00","22:00");
  10. /**
  11. * $todayDate initialized, value set in constructor
  12. * @var dateTime
  13. */
  14. private $todayDate;
  15. /**
  16. * $todayTime again, initialised in constructor, stores the time
  17. * @var dateTime
  18. */
  19. private $todayTime;
  20.  
  21. /**
  22. * Constructor, initialises both time and date
  23. */
  24. public function __construct() {
  25. $this->todayDate = date("Y-m-d");
  26. $this->todayTime = date("H:i:s");
  27. }
  28. /**
  29. * Gives Return class methods
  30. * @return string Output format of class methods
  31. */
  32. public function __toString()
  33. {
  34. $output = "";
  35. $output .= "\nThe following methods are available for " . __CLASS__ . " object: <br>";
  36. $output .= implode("<br>", get_class_methods(__CLASS__));
  37. $output .= "<br>";
  38. return $output;
  39. }
  40.  
  41. /**
  42. * Optional restart time overide from default
  43. * @param array $times array of restart times
  44. */
  45. public function setRestartTimes($times)
  46. {
  47. if (!is_array($times)) {
  48. throw new Exception('Parameter $times must be an array');
  49. }
  50. $this->restartTimes = $times;
  51. }
  52. /**
  53. * User may want to fetch array of restart time for display
  54. * @return array array of restart times
  55. */
  56. public function getRestartTimes()
  57. {
  58. return $this->restartTimes;
  59. }
  60.  
  61. /**
  62. * Will return the next nearest restart time
  63. * @return String Returns the time from array
  64. */
  65. private function findNearestTime(){
  66. foreach ($this->restartTimes as $time) {
  67. if($this->todayTime < $time)
  68. {
  69. return $time;
  70. break;
  71. }
  72. }
  73. }
  74. /**
  75. * Will use the php built in fucntion diff to determine time between current time and next nearest restart time
  76. * @return Object object of time value between now and next restart time
  77. */
  78. private function findTimeDifference(){
  79. $d1 = new dateTime("$this->todayDate . $this->todayTime");
  80. $d2= new DateTime("$this->todayDate ". $this->findNearestTime());
  81. return $d2->diff($d1);
  82. }
  83. /**
  84. * Formats output to display how long until restart in hours and minutes
  85. * @param object $diff object containg time difference data
  86. * @return string returns formatted time before restart
  87. */
  88. private function formatOutput($diff){
  89. if($diff->h > 1 || $diff->h == 0){
  90. $hour = "Server restart in " . $diff->h . " hours, ";
  91. }else{
  92. $hour = "Server restart in " . $diff->h . " hour, ";
  93. }
  94. if($diff->i > 1 || $diff->i == 0){
  95. $minute = $diff->i . " minutes.";
  96. }else{
  97. $minute = $diff->i . " minute.";
  98. }
  99. if($diff->i == 0 && $diff->h == 0){
  100. $output = "<script>$('.restart').addClass('animated bounce infinite');</script>";
  101. $output .= "<h2 class='restart'>Server Restarting</h2>";
  102. return $output;
  103. }elseif($diff->i == 59 && $diff->h == 1){
  104. $output = "<script>$('.restart').addClass('animated bounce infinite');</script>";
  105. $output .= "<h2 class='restart'>Server Restarting</h2>";
  106. return $output;
  107. }else{
  108. return $hour . $minute;
  109. }
  110.  
  111. }
  112. /**
  113. * Allows user to alter current time to account for any clock discrepencies
  114. * @param string $time used to specify time added
  115. * @param string $mode used to determinne whether it should be added or subtracted from current time
  116. */
  117. public function changeTime($time,$mode)
  118. {
  119. $this->todayTime = new DateTime(date("Y-m-d H:i:s"));
  120. if($mode == strtolower("add")){
  121. $this->todayTime->add(new DateInterval($time));
  122. }elseif($mode ==strtolower("sub")){
  123. $this->todayTime->sub(new DateInterval($time));
  124. }
  125. $this->todayTime = $this->todayTime->format('H:i:s');
  126. }
  127. /**
  128. * Simply for debugging, if the user needs to specify a current time, it overides the computer clock.
  129. * @param dateTime $date contains the requested date in the format: "Y-m-d"
  130. * @param dateTie $time contains the requested time
  131. * @return setvaraible sets the corresponding time and date
  132. */
  133. public function testDateTime($date = null, $time = null)
  134. {
  135. if(isset($date) && isset($time)){
  136. $this->todayTime = $time;
  137. $this->todayDate = $date;
  138. }elseif(isset($time)){
  139. $this->todayTime = $time;
  140. }elseif(isset($date)){
  141. $this->todayDate = $date;
  142. }
  143. }
  144.  
  145. /**
  146. * returns the restart Timer
  147. * @return string returns formatted timme difference
  148. */
  149. public function execute()
  150. {
  151. return $this->formatOutput($this->findTimeDifference());
  152. }
  153.  
  154. }
  155.  
  156. $timer = new RestartTimer;
  157. $timer->changeTime('PT2M', 'sub');
  158. echo $timer->execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement