DateInputs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 DateInputs
  53. {
  54. /**#@+
  55. * @ignore
  56. */
  57. /**
  58. * @var string
  59. */
  60. protected $strDate = null;
  61. /**
  62. * @var string
  63. */
  64. protected $strHolidaysFilename = 'Holidays.xml';
  65. /**
  66. * @var SimpleXMLElement
  67. */
  68. protected $objHolidaysXML = null;
  69. /**#@-*/
  70. /**
  71. * @param string $strDate (Default: null)
  72. */
  73. public function __construct($strDate = null)
  74. {
  75. $this->strDate = $strDate;
  76. }
  77. /**
  78. * @param string $strDate
  79. */
  80. public function setDefaultDate($strDate)
  81. {
  82. $this->strDate = $strDate;
  83. }
  84. /**
  85. * @param string $strDate (Default: null)
  86. * @return array
  87. * @uses getDefaultDate()
  88. */
  89. public function getWeek($strDate = null)
  90. {
  91. $arrReturn = array();
  92. if(!$strDate)
  93. $strDate = $this->getDefaultDate();
  94. if($strDate)
  95. {
  96. $intWeek = date('W', strtotime($strDate));
  97. }
  98. else
  99. {
  100. $intWeek = date('W');
  101. }
  102. for($intIndex = 1; $intIndex <= 53; $intIndex++)
  103. $arrReturn[$intIndex] = ($intWeek == $intIndex) ? 1 : 0;
  104. return $arrReturn;
  105. }
  106. /**
  107. * @param string $strDate (Default: null)
  108. * @return array
  109. * @uses getDefaultDate()
  110. */
  111. public function getWeekDay($strDate = null)
  112. {
  113. $arrReturn = array();
  114. if(!$strDate)
  115. $strDate = $this->getDefaultDate();
  116. if($strDate)
  117. {
  118. $intWeekDay = date('w', strtotime($strDate));
  119. }
  120. else
  121. {
  122. $intWeekDay = date('w');
  123. }
  124. if($intWeekDay == 0)
  125. $intWeekDay = 7;
  126. for($intIndex = 1; $intIndex <= 7; $intIndex++)
  127. $arrReturn[$intIndex] = ($intWeekDay == $intIndex) ? 1 : 0;
  128. return $arrReturn;
  129. }
  130. /**
  131. * @param string $strDate (Default: null)
  132. * @return array
  133. * @uses getDefaultDate()
  134. */
  135. public function getYearDay($strDate = null)
  136. {
  137. $arrReturn = array();
  138. if(!$strDate)
  139. $strDate = $this->getDefaultDate();
  140. if($strDate)
  141. {
  142. $intYearDay = date('z', strtotime($strDate));
  143. }
  144. else
  145. {
  146. $intYearDay = date('z');
  147. }
  148. $intYearDay++;
  149. for($intIndex = 1; $intIndex <= 366; $intIndex++)
  150. $arrReturn[$intIndex] = ($intYearDay == $intIndex) ? 1 : 0;
  151. return $arrReturn;
  152. }
  153. /**
  154. * @param string $strDate (Default: null)
  155. * @return array
  156. * @uses getDefaultDate()
  157. */
  158. public function getMonthWeek($strDate = null)
  159. {
  160. $arrReturn = array();
  161. if(!$strDate)
  162. $strDate = $this->getDefaultDate();
  163. if($strDate)
  164. {
  165. $intDay = date('d', strtotime($strDate));
  166. }
  167. else
  168. {
  169. $intDay = date('d');
  170. }
  171. $intWeek = (int)($intDay / 7);
  172. $intWeek++;
  173. for($intIndex = 1; $intIndex <= 5; $intIndex++)
  174. $arrReturn[$intIndex] = ($intWeek == $intIndex) ? 1 : 0;
  175. return $arrReturn;
  176. }
  177. /**
  178. * @param string $strDate (Default: null)
  179. * @return array
  180. * @uses getDefaultDate()
  181. */
  182. public function getQuarter($strDate = null)
  183. {
  184. $arrReturn = array();
  185. if(!$strDate)
  186. $strDate = $this->getDefaultDate();
  187. if($strDate)
  188. {
  189. $intMonth = date('m', strtotime($strDate));
  190. }
  191. else
  192. {
  193. $intMonth = date('m');
  194. }
  195. $intQuarter = (int)($intMonth / 4);
  196. $intQuarter++;
  197. for($intIndex = 1; $intIndex <= 4; $intIndex++)
  198. $arrReturn[$intIndex] = ($intQuarter == $intIndex) ? 1 : 0;
  199. return $arrReturn;
  200. }
  201. /**
  202. * @param string $strDate (Default: null)
  203. * @return array
  204. * @uses getDefaultDate()
  205. */
  206. public function getDaylight($strDate = null)
  207. {
  208. $arrReturn = array();
  209. if(!$strDate)
  210. $strDate = $this->getDefaultDate();
  211. if($strDate)
  212. {
  213. $floatSunrise = date_sunrise(strtotime($strDate), SUNFUNCS_RET_DOUBLE);
  214. $floatSunset = date_sunset(strtotime($strDate), SUNFUNCS_RET_DOUBLE);
  215. }
  216. else
  217. {
  218. $floatSunrise = date_sunrise(time(), SUNFUNCS_RET_DOUBLE);
  219. $floatSunset = date_sunset(time(), SUNFUNCS_RET_DOUBLE);
  220. }
  221. $floatDaylight = ($floatSunset - $floatSunrise) / 24;
  222. return array($floatDaylight);
  223. }
  224. /**
  225. * @param string $strFilename
  226. */
  227. public function setHolidaysFilename($strFilename)
  228. {
  229. $this->strHolidaysFilename = $strFilename;
  230. }
  231. /**
  232. * @param string $strDate (Default: null)
  233. * @return array
  234. * @uses getDefaultDate()
  235. * @uses getDatesOfWeek()
  236. * @uses isHoliday()
  237. */
  238. public function getHolidaysInWeek($strDate = null)
  239. {
  240. if(!$strDate)
  241. $strDate = $this->getDefaultDate(TRUE);
  242. $arrDatesOfWeek = $this->getDatesOfWeek($strDate);
  243. foreach($arrDatesOfWeek as $intKey => $strDateOfWeek)
  244. {
  245. $arrReturn[$intKey] = ($this->isHoliday($strDateOfWeek)) ? 1 : 0;
  246. }
  247. return $arrReturn;
  248. }
  249. /**
  250. * @param boolean $boolCurrentDate
  251. * @return string
  252. */
  253. protected function getDefaultDate($boolCurrentDate = FALSE)
  254. {
  255. if($boolCurrentDate && $this->strDate === null)
  256. return date('Y-m-d');
  257. return $this->strDate;
  258. }
  259. /**
  260. * @param string $strDate (Default: null)
  261. * @return boolean
  262. * @uses getDefaultDate()
  263. * @uses getHolidays()
  264. */
  265. protected function isHoliday($strDate = null)
  266. {
  267. if(!$strDate)
  268. $strDate = $this->getDefaultDate(TRUE);
  269. $this->getHolidays();
  270. $arrDate = explode('-', $strDate);
  271. $strDay = (int)$arrDate[2];
  272. $strMonth = (int)$arrDate[1];
  273. $strYear = (int)$arrDate[0];
  274. if($this->objHolidaysXML instanceof SimpleXMLElement)
  275. foreach($this->objHolidaysXML->holiday as $objHoliday)
  276. {
  277. if($objHoliday->day == $strDay
  278. && $objHoliday->month == $strMonth
  279. && $objHoliday->year == $strYear)
  280. return TRUE;
  281. }
  282. if($this->objHolidaysXML instanceof SimpleXMLElement)
  283. foreach($this->objHolidaysXML->holiday as $objHoliday)
  284. {
  285. if($objHoliday->day == $strDay
  286. && $objHoliday->month == $strMonth
  287. && $objHoliday->year == 'any')
  288. return TRUE;
  289. }
  290. }
  291. /**
  292. * @uses SimpleXMLElement::__construct()
  293. * @throws Exception
  294. */
  295. protected function getHolidays()
  296. {
  297. if($this->objHolidaysXML instanceof SimpleXMLElement)
  298. return;
  299. if(!is_file($this->strHolidaysFilename))
  300. throw new Exception('File '. $this->strHolidaysFilename .' does not exist');
  301. if(!is_readable($this->strHolidaysFilename))
  302. throw new Exception('File '. $this->strHolidaysFilename .' does not have read permission');
  303. $strXML = @file_get_contents($this->strHolidaysFilename);
  304. try
  305. {
  306. $this->objHolidaysXML = new SimpleXMLElement($strXML);
  307. }
  308. catch(Exception $e)
  309. {
  310. throw new Exception($e->getMessage());
  311. }
  312. if(!($this->objHolidaysXML instanceof SimpleXMLElement))
  313. throw new Exception('XML Object cannot be created');
  314. if(!isset($this->objHolidaysXML->holiday))
  315. throw new Exception('Missing at least on holiday element');
  316. $intElementIndex = 0;
  317. foreach($this->objHolidaysXML->holiday as $objHoliday)
  318. {
  319. $intElementIndex++;
  320. if(!isset($objHoliday->day))
  321. throw new Exception('Missing day element in holiday element '. $intElementIndex);
  322. if(!isset($objHoliday->month))
  323. throw new Exception('Missing month element in holiday element '. $intElementIndex);
  324. if(!isset($objHoliday->year))
  325. throw new Exception('Missing year element in holiday element '. $intElementIndex);
  326. if(!isset($objHoliday->country))
  327. throw new Exception('Missing country element in holiday element '. $intElementIndex);
  328. if(!isset($objHoliday->state))
  329. throw new Exception('Missing state element in holiday element '. $intElementIndex);
  330. if(!isset($objHoliday->description))
  331. throw new Exception('Missing descrition element in holiday element '. $intElementIndex);
  332. }
  333. }
  334. /**
  335. * @param string $strDate (Default: null)
  336. * @return array
  337. * @uses getDefaultDate()
  338. * @uses getFirstDayOfWeek()
  339. * @uses getNextDayOfWeek()
  340. */
  341. protected function getDatesOfWeek($strDate = null)
  342. {
  343. if(!$strDate)
  344. $strDate = $this->getDefaultDate(TRUE);
  345. $strDateMonday = $this->getFirstDayOfWeek($strDate);
  346. $strDateTuesday = $this->getNextDayOfWeek($strDateMonday, 1);
  347. $strDateWednesday = $this->getNextDayOfWeek($strDateMonday, 2);
  348. $strDateThursday = $this->getNextDayOfWeek($strDateMonday, 3);
  349. $strDateFriday = $this->getNextDayOfWeek($strDateMonday, 4);
  350. $strDateSaturday = $this->getNextDayOfWeek($strDateMonday, 5);
  351. $strDateSunday = $this->getNextDayOfWeek($strDateMonday, 6);
  352. $arrReturn = array(
  353. 1 => $strDateMonday,
  354. 2 => $strDateTuesday,
  355. 3 => $strDateWednesday,
  356. 4 => $strDateThursday,
  357. 5 => $strDateFriday,
  358. 6 => $strDateSaturday,
  359. 7 => $strDateSunday
  360. );
  361. return $arrReturn;
  362. }
  363. /**
  364. * @param string $strDate (Default: null)
  365. * @return string
  366. * @uses getDefaultDate()
  367. */
  368. protected function getFirstDayOfWeek($strDate)
  369. {
  370. if(!$strDate)
  371. $strDate = $this->getDefaultDate(TRUE);
  372. $intDayOfWeek = date('w', strtotime($strDate));
  373. if($intDayOfWeek == 0)
  374. $intDayOfWeek = 7;
  375. $intUnixTime = strtotime($strDate) - ($intDayOfWeek - 1) * 86400;
  376. return date('Y-m-d', $intUnixTime);
  377. }
  378. /**
  379. * @param string $strDate
  380. * @param integer $intDayIncrement
  381. * @return string
  382. * @uses getDefaultDate()
  383. */
  384. protected function getNextDayOfWeek($strDate, $intDayIncrement)
  385. {
  386. if(!$strDate)
  387. $strDate = $this->getDefaultDate(TRUE);
  388. $intUnixTime = strtotime($strDate) + $intDayIncrement * 86400;
  389. return date('Y-m-d', $intUnixTime);
  390. }
  391. }