TimeInputs.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Artificial Neural Network - Version 2.2
  4. *
  5. * For updates and changes visit the project page at http://ann.thwien.de/
  6. *
  7. *
  8. *
  9. * <b>LICENCE</b>
  10. *
  11. * The BSD 2-Clause License
  12. *
  13. * http://opensource.org/licenses/bsd-license.php
  14. *
  15. * Copyright (c) 2007 - 2012, Thomas Wien
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. *
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. *
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  32. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  33. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  35. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  39. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * @author Thomas Wien <info_at_thwien_dot_de>
  43. * @version ANN Version 2.2 by Thomas Wien
  44. * @copyright Copyright (c) 2007-2012 by Thomas Wien
  45. * @package ANN
  46. */
  47. namespace ANN;
  48. /**
  49. * @package ANN
  50. * @access public
  51. */
  52. final class TimeInputs
  53. {
  54. /**#@+
  55. * @ignore
  56. */
  57. /**
  58. * @var string
  59. */
  60. protected $strTime = null;
  61. /**#@-*/
  62. /**
  63. * @param string $strTime (Default: null)
  64. * @uses checkTimeFormat()
  65. * @throws Exception
  66. */
  67. public function __construct($strTime = null)
  68. {
  69. if($strTime && !$this->checkTimeFormat($strTime))
  70. throw new Exception('Constraints: $strTime should be HH:MM format');
  71. $this->strTime = $strTime;
  72. }
  73. /**
  74. * @param string $strTime
  75. * @uses checkTimeFormat()
  76. * @throws Exception
  77. */
  78. public function setDefaultTime($strTime)
  79. {
  80. if(!$this->checkTimeFormat($strTime))
  81. throw new Exception('Constraints: $strTime should be HH:MM format');
  82. $this->strTime = $strTime;
  83. }
  84. /**
  85. * @param string $strTime (Default: null)
  86. * @return array
  87. * @uses checkTimeFormat()
  88. * @throws Exception
  89. */
  90. public function getTimeOfDay($strTime = null)
  91. {
  92. if(!$strTime)
  93. $strTime = $this->getDefaultTime();
  94. if(!$this->checkTimeFormat($strTime))
  95. throw new Exception('Constraints: $strTime should be HH:MM format');
  96. $arrReturn = array();
  97. $intHour = date('G', strtotime($strTime));
  98. $arrReturn[0] = ($intHour < 6) ? 1 : 0;
  99. $arrReturn[1] = ($intHour >= 6 && $intHour < 12) ? 1 : 0;
  100. $arrReturn[2] = ($intHour >= 12 && $intHour < 18) ? 1 : 0;
  101. $arrReturn[3] = ($intHour >= 18) ? 1 : 0;
  102. return $arrReturn;
  103. }
  104. /**
  105. * @param string $strTime (Default: null)
  106. * @return array
  107. * @uses checkTimeFormat()
  108. * @throws Exception
  109. */
  110. public function getHour($strTime = null)
  111. {
  112. if(!$strTime)
  113. $strTime = $this->getDefaultTime();
  114. if(!$this->checkTimeFormat($strTime))
  115. throw new Exception('Constraints: $strTime should be HH:MM format');
  116. for($intHour = 0; $intHour <= 23; $intHour++)
  117. $arrReturn[$intHour] = 0;
  118. $intHour = date('G', strtotime($strTime));
  119. $arrReturn[$intHour] = 1;
  120. return $arrReturn;
  121. }
  122. /**
  123. * @return string
  124. */
  125. protected function getDefaultTime()
  126. {
  127. if(!$this->strTime)
  128. return date('H:i');
  129. return $this->strTime;
  130. }
  131. /**
  132. * @param string $strTime
  133. * @return boolean
  134. */
  135. protected function checkTimeFormat($strTime)
  136. {
  137. return preg_match('/^[0-2][0-9]:[0-5][0-9]$/', $strTime);
  138. }
  139. }