PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Shared/Date.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 350 lines | 196 code | 40 blank | 114 comment | 32 complexity | 476598f048dcf8421753aeba089a31e0 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Shared
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Shared_Date
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Shared_Date
  35. {
  36. /** constants */
  37. const CALENDAR_WINDOWS_1900 = 1900; // Base date of 1st Jan 1900 = 1.0
  38. const CALENDAR_MAC_1904 = 1904; // Base date of 2nd Jan 1904 = 1.0
  39. private static $ExcelBaseDate = self :: CALENDAR_WINDOWS_1900;
  40. public static $dateTimeObjectType = 'DateTime';
  41. /**
  42. * Set the Excel calendar (Windows 1900 or Mac 1904)
  43. *
  44. * @param integer $baseDate Excel base date
  45. * @return boolean Success or failure
  46. */
  47. public static function setExcelCalendar($baseDate)
  48. {
  49. if (($baseDate == self :: CALENDAR_WINDOWS_1900) || ($baseDate == self :: CALENDAR_MAC_1904))
  50. {
  51. self :: $ExcelBaseDate = $baseDate;
  52. return True;
  53. }
  54. return False;
  55. } // function setExcelCalendar()
  56. /**
  57. * Return the Excel calendar (Windows 1900 or Mac 1904)
  58. *
  59. * @return integer $baseDate Excel base date
  60. */
  61. public static function getExcelCalendar()
  62. {
  63. return self :: $ExcelBaseDate;
  64. } // function getExcelCalendar()
  65. /**
  66. * Convert a date from Excel to PHP
  67. *
  68. * @param long $dateValue Excel date/time value
  69. * @return long PHP serialized date/time
  70. */
  71. public static function ExcelToPHP($dateValue = 0)
  72. {
  73. if (self :: $ExcelBaseDate == self :: CALENDAR_WINDOWS_1900)
  74. {
  75. $myExcelBaseDate = 25569;
  76. // Adjust for the spurious 29-Feb-1900 (Day 60)
  77. if ($dateValue < 60)
  78. {
  79. -- $myExcelBaseDate;
  80. }
  81. }
  82. else
  83. {
  84. $myExcelBaseDate = 24107;
  85. }
  86. // Perform conversion
  87. if ($dateValue >= 1)
  88. {
  89. $utcDays = $dateValue - $myExcelBaseDate;
  90. $returnValue = round($utcDays * 24 * 60 * 60);
  91. if (($returnValue <= PHP_INT_MAX) && ($returnValue >= - PHP_INT_MAX))
  92. {
  93. $returnValue = (integer) $returnValue;
  94. }
  95. }
  96. else
  97. {
  98. $hours = round($dateValue * 24);
  99. $mins = round($dateValue * 24 * 60) - round($hours * 60);
  100. $secs = round($dateValue * 24 * 60 * 60) - round($hours * 60 * 60) - round($mins * 60);
  101. $returnValue = (integer) gmmktime($hours, $mins, $secs);
  102. }
  103. // Return
  104. return $returnValue;
  105. } // function ExcelToPHP()
  106. /**
  107. * Convert a date from Excel to a PHP Date/Time object
  108. *
  109. * @param long $dateValue Excel date/time value
  110. * @return long PHP date/time object
  111. */
  112. public static function ExcelToPHPObject($dateValue = 0)
  113. {
  114. $dateTime = self :: ExcelToPHP($dateValue);
  115. $days = floor($dateTime / 86400);
  116. $time = round((($dateTime / 86400) - $days) * 86400);
  117. $hours = round($time / 3600);
  118. $minutes = round($time / 60) - ($hours * 60);
  119. $seconds = round($time) - ($hours * 3600) - ($minutes * 60);
  120. $dateObj = date_create('1-Jan-1970+' . $days . ' days');
  121. $dateObj->setTime($hours, $minutes, $seconds);
  122. return $dateObj;
  123. } // function ExcelToPHPObject()
  124. /**
  125. * Convert a date from PHP to Excel
  126. *
  127. * @param mixed $dateValue PHP serialized date/time or date object
  128. * @return mixed Excel date/time value
  129. * or boolean False on failure
  130. */
  131. public static function PHPToExcel($dateValue = 0)
  132. {
  133. $saveTimeZone = date_default_timezone_get();
  134. date_default_timezone_set('UTC');
  135. $retValue = False;
  136. if ((is_object($dateValue)) && ($dateValue instanceof self :: $dateTimeObjectType))
  137. {
  138. $retValue = self :: FormattedPHPToExcel($dateValue->format('Y'), $dateValue->format('m'), $dateValue->format('d'), $dateValue->format('H'), $dateValue->format('i'), $dateValue->format('s'));
  139. }
  140. elseif (is_numeric($dateValue))
  141. {
  142. $retValue = self :: FormattedPHPToExcel(date('Y', $dateValue), date('m', $dateValue), date('d', $dateValue), date('H', $dateValue), date('i', $dateValue), date('s', $dateValue));
  143. }
  144. date_default_timezone_set($saveTimeZone);
  145. return $retValue;
  146. } // function PHPToExcel()
  147. /**
  148. * FormattedPHPToExcel
  149. *
  150. * @param long $year
  151. * @param long $month
  152. * @param long $day
  153. * @param long $hours
  154. * @param long $minutes
  155. * @param long $seconds
  156. * @return long Excel date/time value
  157. */
  158. public static function FormattedPHPToExcel($year, $month, $day, $hours = 0, $minutes = 0, $seconds = 0)
  159. {
  160. if (self :: $ExcelBaseDate == self :: CALENDAR_WINDOWS_1900)
  161. {
  162. //
  163. // Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel
  164. // This affects every date following 28th February 1900
  165. //
  166. $excel1900isLeapYear = True;
  167. if (($year == 1900) && ($month <= 2))
  168. {
  169. $excel1900isLeapYear = False;
  170. }
  171. $myExcelBaseDate = 2415020;
  172. }
  173. else
  174. {
  175. $myExcelBaseDate = 2416481;
  176. $excel1900isLeapYear = False;
  177. }
  178. // Julian base date Adjustment
  179. if ($month > 2)
  180. {
  181. $month = $month - 3;
  182. }
  183. else
  184. {
  185. $month = $month + 9;
  186. -- $year;
  187. }
  188. // Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
  189. $century = substr($year, 0, 2);
  190. $decade = substr($year, 2, 2);
  191. $excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear;
  192. $excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
  193. return (float) $excelDate + $excelTime;
  194. } // function FormattedPHPToExcel()
  195. /**
  196. * Is a given cell a date/time?
  197. *
  198. * @param PHPExcel_Cell $pCell
  199. * @return boolean
  200. */
  201. public static function isDateTime(PHPExcel_Cell $pCell)
  202. {
  203. return self :: isDateTimeFormat($pCell->getParent()->getStyle($pCell->getCoordinate())->getNumberFormat());
  204. } // function isDateTime()
  205. /**
  206. * Is a given number format a date/time?
  207. *
  208. * @param PHPExcel_Style_NumberFormat $pFormat
  209. * @return boolean
  210. */
  211. public static function isDateTimeFormat(PHPExcel_Style_NumberFormat $pFormat)
  212. {
  213. return self :: isDateTimeFormatCode($pFormat->getFormatCode());
  214. } // function isDateTimeFormat()
  215. private static $possibleDateFormatCharacters = 'ymdHs';
  216. /**
  217. * Is a given number format code a date/time?
  218. *
  219. * @param string $pFormatCode
  220. * @return boolean
  221. */
  222. public static function isDateTimeFormatCode($pFormatCode = '')
  223. {
  224. // Switch on formatcode
  225. switch ($pFormatCode)
  226. {
  227. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_YYYYMMDD :
  228. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_YYYYMMDD2 :
  229. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_DDMMYYYY :
  230. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_DMYSLASH :
  231. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_DMYMINUS :
  232. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_DMMINUS :
  233. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_MYMINUS :
  234. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_DATETIME :
  235. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_TIME1 :
  236. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_TIME2 :
  237. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_TIME3 :
  238. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_TIME4 :
  239. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_TIME5 :
  240. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_TIME6 :
  241. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_TIME7 :
  242. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_TIME8 :
  243. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_YYYYMMDDSLASH :
  244. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_XLSX14 :
  245. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_XLSX15 :
  246. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_XLSX16 :
  247. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_XLSX17 :
  248. case PHPExcel_Style_NumberFormat :: FORMAT_DATE_XLSX22 :
  249. return true;
  250. }
  251. // Typically number, currency or accounting (or occasionally fraction) formats
  252. if ((substr($pFormatCode, 0, 1) == '_') || (substr($pFormatCode, 0, 2) == '0 '))
  253. {
  254. return false;
  255. }
  256. // Try checking for any of the date formatting characters that don't appear within square braces
  257. if (preg_match('/(^|\])[^\[]*[' . self :: $possibleDateFormatCharacters . ']/i', $pFormatCode))
  258. {
  259. // We might also have a format mask containing quoted strings...
  260. // we don't want to test for any of our characters within the quoted blocks
  261. if (strpos($pFormatCode, '"') !== false)
  262. {
  263. $i = false;
  264. foreach (explode('"', $pFormatCode) as $subVal)
  265. {
  266. // Only test in alternate array entries (the non-quoted blocks)
  267. if (($i = ! $i) && (preg_match('/(^|\])[^\[]*[' . self :: $possibleDateFormatCharacters . ']/i', $subVal)))
  268. {
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. return true;
  275. }
  276. // No date...
  277. return false;
  278. } // function isDateTimeFormatCode()
  279. /**
  280. * Convert a date/time string to Excel time
  281. *
  282. * @param string $dateValue Examples: '2009-12-31', '2009-12-31 15:59', '2009-12-31 15:59:10'
  283. * @return float|false Excel date/time serial value
  284. */
  285. public static function stringToExcel($dateValue = '')
  286. {
  287. if (strlen($dateValue) < 2)
  288. return false;
  289. if (! preg_match('/^(\d{1,4}[ \.\/\-][A-Z]{3,9}([ \.\/\-]\d{1,4})?|[A-Z]{3,9}[ \.\/\-]\d{1,4}([ \.\/\-]\d{1,4})?|\d{1,4}[ \.\/\-]\d{1,4}([ \.\/\-]\d{1,4})?)( \d{1,2}:\d{1,2}(:\d{1,2})?)?$/iu', $dateValue))
  290. return false;
  291. $dateValueNew = PHPExcel_Calculation_DateTime :: DATEVALUE($dateValue);
  292. if ($dateValueNew === PHPExcel_Calculation_Functions :: VALUE())
  293. {
  294. return false;
  295. }
  296. else
  297. {
  298. if (strpos($dateValue, ':') !== false)
  299. {
  300. $timeValue = PHPExcel_Calculation_DateTime :: TIMEVALUE($dateValue);
  301. if ($timeValue === PHPExcel_Calculation_Functions :: VALUE())
  302. {
  303. return false;
  304. }
  305. $dateValueNew += $timeValue;
  306. }
  307. return $dateValueNew;
  308. }
  309. }
  310. }