PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/05_Desarrollo/lib/adodb/adodb-time.inc.php

https://bitbucket.org/SerafinAkatsuki/consultorio
PHP | 1426 lines | 738 code | 150 blank | 538 comment | 249 complexity | 2218ec5dbc7d23ac8e368682c97e2673 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. ADOdb Date Library, part of the ADOdb abstraction library
  4. Download: http://phplens.com/phpeverywhere/
  5. PHP native date functions use integer timestamps for computations.
  6. Because of this, dates are restricted to the years 1901-2038 on Unix
  7. and 1970-2038 on Windows due to integer overflow for dates beyond
  8. those years. This library overcomes these limitations by replacing the
  9. native function's signed integers (normally 32-bits) with PHP floating
  10. point numbers (normally 64-bits).
  11. Dates from 100 A.D. to 3000 A.D. and later
  12. have been tested. The minimum is 100 A.D. as <100 will invoke the
  13. 2 => 4 digit year conversion. The maximum is billions of years in the
  14. future, but this is a theoretical limit as the computation of that year
  15. would take too long with the current implementation of adodb_mktime().
  16. This library replaces native functions as follows:
  17. <pre>
  18. getdate() with adodb_getdate()
  19. date() with adodb_date()
  20. gmdate() with adodb_gmdate()
  21. mktime() with adodb_mktime()
  22. gmmktime() with adodb_gmmktime()
  23. strftime() with adodb_strftime()
  24. strftime() with adodb_gmstrftime()
  25. </pre>
  26. The parameters are identical, except that adodb_date() accepts a subset
  27. of date()'s field formats. Mktime() will convert from local time to GMT,
  28. and date() will convert from GMT to local time, but daylight savings is
  29. not handled currently.
  30. This library is independant of the rest of ADOdb, and can be used
  31. as standalone code.
  32. PERFORMANCE
  33. For high speed, this library uses the native date functions where
  34. possible, and only switches to PHP code when the dates fall outside
  35. the 32-bit signed integer range.
  36. GREGORIAN CORRECTION
  37. Pope Gregory shortened October of A.D. 1582 by ten days. Thursday,
  38. October 4, 1582 (Julian) was followed immediately by Friday, October 15,
  39. 1582 (Gregorian).
  40. Since 0.06, we handle this correctly, so:
  41. adodb_mktime(0,0,0,10,15,1582) - adodb_mktime(0,0,0,10,4,1582)
  42. == 24 * 3600 (1 day)
  43. =============================================================================
  44. COPYRIGHT
  45. (c) 2003-2005 John Lim and released under BSD-style license except for code by
  46. jackbbs, which includes adodb_mktime, adodb_get_gmt_diff, adodb_is_leap_year
  47. and originally found at http://www.php.net/manual/en/function.mktime.php
  48. =============================================================================
  49. BUG REPORTS
  50. These should be posted to the ADOdb forums at
  51. http://phplens.com/lens/lensforum/topics.php?id=4
  52. =============================================================================
  53. FUNCTION DESCRIPTIONS
  54. ** FUNCTION adodb_getdate($date=false)
  55. Returns an array containing date information, as getdate(), but supports
  56. dates greater than 1901 to 2038. The local date/time format is derived from a
  57. heuristic the first time adodb_getdate is called.
  58. ** FUNCTION adodb_date($fmt, $timestamp = false)
  59. Convert a timestamp to a formatted local date. If $timestamp is not defined, the
  60. current timestamp is used. Unlike the function date(), it supports dates
  61. outside the 1901 to 2038 range.
  62. The format fields that adodb_date supports:
  63. <pre>
  64. a - "am" or "pm"
  65. A - "AM" or "PM"
  66. d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
  67. D - day of the week, textual, 3 letters; e.g. "Fri"
  68. F - month, textual, long; e.g. "January"
  69. g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
  70. G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
  71. h - hour, 12-hour format; i.e. "01" to "12"
  72. H - hour, 24-hour format; i.e. "00" to "23"
  73. i - minutes; i.e. "00" to "59"
  74. j - day of the month without leading zeros; i.e. "1" to "31"
  75. l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
  76. L - boolean for whether it is a leap year; i.e. "0" or "1"
  77. m - month; i.e. "01" to "12"
  78. M - month, textual, 3 letters; e.g. "Jan"
  79. n - month without leading zeros; i.e. "1" to "12"
  80. O - Difference to Greenwich time in hours; e.g. "+0200"
  81. Q - Quarter, as in 1, 2, 3, 4
  82. r - RFC 2822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200"
  83. s - seconds; i.e. "00" to "59"
  84. S - English ordinal suffix for the day of the month, 2 characters;
  85. i.e. "st", "nd", "rd" or "th"
  86. t - number of days in the given month; i.e. "28" to "31"
  87. T - Timezone setting of this machine; e.g. "EST" or "MDT"
  88. U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  89. w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
  90. Y - year, 4 digits; e.g. "1999"
  91. y - year, 2 digits; e.g. "99"
  92. z - day of the year; i.e. "0" to "365"
  93. Z - timezone offset in seconds (i.e. "-43200" to "43200").
  94. The offset for timezones west of UTC is always negative,
  95. and for those east of UTC is always positive.
  96. </pre>
  97. Unsupported:
  98. <pre>
  99. B - Swatch Internet time
  100. I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
  101. W - ISO-8601 week number of year, weeks starting on Monday
  102. </pre>
  103. ** FUNCTION adodb_date2($fmt, $isoDateString = false)
  104. Same as adodb_date, but 2nd parameter accepts iso date, eg.
  105. adodb_date2('d-M-Y H:i','2003-12-25 13:01:34');
  106. ** FUNCTION adodb_gmdate($fmt, $timestamp = false)
  107. Convert a timestamp to a formatted GMT date. If $timestamp is not defined, the
  108. current timestamp is used. Unlike the function date(), it supports dates
  109. outside the 1901 to 2038 range.
  110. ** FUNCTION adodb_mktime($hr, $min, $sec[, $month, $day, $year])
  111. Converts a local date to a unix timestamp. Unlike the function mktime(), it supports
  112. dates outside the 1901 to 2038 range. All parameters are optional.
  113. ** FUNCTION adodb_gmmktime($hr, $min, $sec [, $month, $day, $year])
  114. Converts a gmt date to a unix timestamp. Unlike the function gmmktime(), it supports
  115. dates outside the 1901 to 2038 range. Differs from gmmktime() in that all parameters
  116. are currently compulsory.
  117. ** FUNCTION adodb_gmstrftime($fmt, $timestamp = false)
  118. Convert a timestamp to a formatted GMT date.
  119. ** FUNCTION adodb_strftime($fmt, $timestamp = false)
  120. Convert a timestamp to a formatted local date. Internally converts $fmt into
  121. adodb_date format, then echo result.
  122. For best results, you can define the local date format yourself. Define a global
  123. variable $ADODB_DATE_LOCALE which is an array, 1st element is date format using
  124. adodb_date syntax, and 2nd element is the time format, also in adodb_date syntax.
  125. eg. $ADODB_DATE_LOCALE = array('d/m/Y','H:i:s');
  126. Supported format codes:
  127. <pre>
  128. %a - abbreviated weekday name according to the current locale
  129. %A - full weekday name according to the current locale
  130. %b - abbreviated month name according to the current locale
  131. %B - full month name according to the current locale
  132. %c - preferred date and time representation for the current locale
  133. %d - day of the month as a decimal number (range 01 to 31)
  134. %D - same as %m/%d/%y
  135. %e - day of the month as a decimal number, a single digit is preceded by a space (range ' 1' to '31')
  136. %h - same as %b
  137. %H - hour as a decimal number using a 24-hour clock (range 00 to 23)
  138. %I - hour as a decimal number using a 12-hour clock (range 01 to 12)
  139. %m - month as a decimal number (range 01 to 12)
  140. %M - minute as a decimal number
  141. %n - newline character
  142. %p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale
  143. %r - time in a.m. and p.m. notation
  144. %R - time in 24 hour notation
  145. %S - second as a decimal number
  146. %t - tab character
  147. %T - current time, equal to %H:%M:%S
  148. %x - preferred date representation for the current locale without the time
  149. %X - preferred time representation for the current locale without the date
  150. %y - year as a decimal number without a century (range 00 to 99)
  151. %Y - year as a decimal number including the century
  152. %Z - time zone or name or abbreviation
  153. %% - a literal `%' character
  154. </pre>
  155. Unsupported codes:
  156. <pre>
  157. %C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
  158. %g - like %G, but without the century.
  159. %G - The 4-digit year corresponding to the ISO week number (see %V).
  160. This has the same format and value as %Y, except that if the ISO week number belongs
  161. to the previous or next year, that year is used instead.
  162. %j - day of the year as a decimal number (range 001 to 366)
  163. %u - weekday as a decimal number [1,7], with 1 representing Monday
  164. %U - week number of the current year as a decimal number, starting
  165. with the first Sunday as the first day of the first week
  166. %V - The ISO 8601:1988 week number of the current year as a decimal number,
  167. range 01 to 53, where week 1 is the first week that has at least 4 days in the
  168. current year, and with Monday as the first day of the week. (Use %G or %g for
  169. the year component that corresponds to the week number for the specified timestamp.)
  170. %w - day of the week as a decimal, Sunday being 0
  171. %W - week number of the current year as a decimal number, starting with the
  172. first Monday as the first day of the first week
  173. </pre>
  174. =============================================================================
  175. NOTES
  176. Useful url for generating test timestamps:
  177. http://www.4webhelp.net/us/timestamp.php
  178. Possible future optimizations include
  179. a. Using an algorithm similar to Plauger's in "The Standard C Library"
  180. (page 428, xttotm.c _Ttotm() function). Plauger's algorithm will not
  181. work outside 32-bit signed range, so i decided not to implement it.
  182. b. Implement daylight savings, which looks awfully complicated, see
  183. http://webexhibits.org/daylightsaving/
  184. CHANGELOG
  185. - 11 Feb 2008 0.33
  186. * Bug in 0.32 fix for hour handling. Fixed.
  187. - 1 Feb 2008 0.32
  188. * Now adodb_mktime(0,0,0,12+$m,20,2040) works properly.
  189. - 10 Jan 2008 0.31
  190. * Now adodb_mktime(0,0,0,24,1,2037) works correctly.
  191. - 15 July 2007 0.30
  192. Added PHP 5.2.0 compatability fixes.
  193. * gmtime behaviour for 1970 has changed. We use the actual date if it is between 1970 to 2038 to get the
  194. * timezone, otherwise we use the current year as the baseline to retrieve the timezone.
  195. * Also the timezone's in php 5.2.* support historical data better, eg. if timezone today was +8, but
  196. in 1970 it was +7:30, then php 5.2 return +7:30, while this library will use +8.
  197. *
  198. - 19 March 2006 0.24
  199. Changed strftime() locale detection, because some locales prepend the day of week to the date when %c is used.
  200. - 10 Feb 2006 0.23
  201. PHP5 compat: when we detect PHP5, the RFC2822 format for gmt 0000hrs is changed from -0000 to +0000.
  202. In PHP4, we will still use -0000 for 100% compat with PHP4.
  203. - 08 Sept 2005 0.22
  204. In adodb_date2(), $is_gmt not supported properly. Fixed.
  205. - 18 July 2005 0.21
  206. In PHP 4.3.11, the 'r' format has changed. Leading 0 in day is added. Changed for compat.
  207. Added support for negative months in adodb_mktime().
  208. - 24 Feb 2005 0.20
  209. Added limited strftime/gmstrftime support. x10 improvement in performance of adodb_date().
  210. - 21 Dec 2004 0.17
  211. In adodb_getdate(), the timestamp was accidentally converted to gmt when $is_gmt is false.
  212. Also adodb_mktime(0,0,0) did not work properly. Both fixed thx Mauro.
  213. - 17 Nov 2004 0.16
  214. Removed intval typecast in adodb_mktime() for secs, allowing:
  215. adodb_mktime(0,0,0 + 2236672153,1,1,1934);
  216. Suggested by Ryan.
  217. - 18 July 2004 0.15
  218. All params in adodb_mktime were formerly compulsory. Now only the hour, min, secs is compulsory.
  219. This brings it more in line with mktime (still not identical).
  220. - 23 June 2004 0.14
  221. Allow you to define your own daylights savings function, adodb_daylight_sv.
  222. If the function is defined (somewhere in an include), then you can correct for daylights savings.
  223. In this example, we apply daylights savings in June or July, adding one hour. This is extremely
  224. unrealistic as it does not take into account time-zone, geographic location, current year.
  225. function adodb_daylight_sv(&$arr, $is_gmt)
  226. {
  227. if ($is_gmt) return;
  228. $m = $arr['mon'];
  229. if ($m == 6 || $m == 7) $arr['hours'] += 1;
  230. }
  231. This is only called by adodb_date() and not by adodb_mktime().
  232. The format of $arr is
  233. Array (
  234. [seconds] => 0
  235. [minutes] => 0
  236. [hours] => 0
  237. [mday] => 1 # day of month, eg 1st day of the month
  238. [mon] => 2 # month (eg. Feb)
  239. [year] => 2102
  240. [yday] => 31 # days in current year
  241. [leap] => # true if leap year
  242. [ndays] => 28 # no of days in current month
  243. )
  244. - 28 Apr 2004 0.13
  245. Fixed adodb_date to properly support $is_gmt. Thx to Dimitar Angelov.
  246. - 20 Mar 2004 0.12
  247. Fixed month calculation error in adodb_date. 2102-June-01 appeared as 2102-May-32.
  248. - 26 Oct 2003 0.11
  249. Because of daylight savings problems (some systems apply daylight savings to
  250. January!!!), changed adodb_get_gmt_diff() to ignore daylight savings.
  251. - 9 Aug 2003 0.10
  252. Fixed bug with dates after 2038.
  253. See http://phplens.com/lens/lensforum/msgs.php?id=6980
  254. - 1 July 2003 0.09
  255. Added support for Q (Quarter).
  256. Added adodb_date2(), which accepts ISO date in 2nd param
  257. - 3 March 2003 0.08
  258. Added support for 'S' adodb_date() format char. Added constant ADODB_ALLOW_NEGATIVE_TS
  259. if you want PHP to handle negative timestamps between 1901 to 1969.
  260. - 27 Feb 2003 0.07
  261. All negative numbers handled by adodb now because of RH 7.3+ problems.
  262. See http://bugs.php.net/bug.php?id=20048&edit=2
  263. - 4 Feb 2003 0.06
  264. Fixed a typo, 1852 changed to 1582! This means that pre-1852 dates
  265. are now correctly handled.
  266. - 29 Jan 2003 0.05
  267. Leap year checking differs under Julian calendar (pre 1582). Also
  268. leap year code optimized by checking for most common case first.
  269. We also handle month overflow correctly in mktime (eg month set to 13).
  270. Day overflow for less than one month's days is supported.
  271. - 28 Jan 2003 0.04
  272. Gregorian correction handled. In PHP5, we might throw an error if
  273. mktime uses invalid dates around 5-14 Oct 1582. Released with ADOdb 3.10.
  274. Added limbo 5-14 Oct 1582 check, when we set to 15 Oct 1582.
  275. - 27 Jan 2003 0.03
  276. Fixed some more month problems due to gmt issues. Added constant ADODB_DATE_VERSION.
  277. Fixed calculation of days since start of year for <1970.
  278. - 27 Jan 2003 0.02
  279. Changed _adodb_getdate() to inline leap year checking for better performance.
  280. Fixed problem with time-zones west of GMT +0000.
  281. - 24 Jan 2003 0.01
  282. First implementation.
  283. */
  284. /* Initialization */
  285. /*
  286. Version Number
  287. */
  288. define('ADODB_DATE_VERSION',0.33);
  289. $ADODB_DATETIME_CLASS = (PHP_VERSION >= 5.2);
  290. /*
  291. This code was originally for windows. But apparently this problem happens
  292. also with Linux, RH 7.3 and later!
  293. glibc-2.2.5-34 and greater has been changed to return -1 for dates <
  294. 1970. This used to work. The problem exists with RedHat 7.3 and 8.0
  295. echo (mktime(0, 0, 0, 1, 1, 1960)); // prints -1
  296. References:
  297. http://bugs.php.net/bug.php?id=20048&edit=2
  298. http://lists.debian.org/debian-glibc/2002/debian-glibc-200205/msg00010.html
  299. */
  300. if (!defined('ADODB_ALLOW_NEGATIVE_TS')) define('ADODB_NO_NEGATIVE_TS',1);
  301. function adodb_date_test_date($y1,$m,$d=13)
  302. {
  303. $h = round(rand()% 24);
  304. $t = adodb_mktime($h,0,0,$m,$d,$y1);
  305. $rez = adodb_date('Y-n-j H:i:s',$t);
  306. if ($h == 0) $h = '00';
  307. else if ($h < 10) $h = '0'.$h;
  308. if ("$y1-$m-$d $h:00:00" != $rez) {
  309. print "<b>$y1 error, expected=$y1-$m-$d $h:00:00, adodb=$rez</b><br>";
  310. return false;
  311. }
  312. return true;
  313. }
  314. function adodb_date_test_strftime($fmt)
  315. {
  316. $s1 = strftime($fmt);
  317. $s2 = adodb_strftime($fmt);
  318. if ($s1 == $s2) return true;
  319. echo "error for $fmt, strftime=$s1, adodb=$s2<br>";
  320. return false;
  321. }
  322. /**
  323. Test Suite
  324. */
  325. function adodb_date_test()
  326. {
  327. for ($m=-24; $m<=24; $m++)
  328. echo "$m :",adodb_date('d-m-Y',adodb_mktime(0,0,0,1+$m,20,2040)),"<br>";
  329. error_reporting(E_ALL);
  330. print "<h4>Testing adodb_date and adodb_mktime. version=".ADODB_DATE_VERSION.' PHP='.PHP_VERSION."</h4>";
  331. @set_time_limit(0);
  332. $fail = false;
  333. // This flag disables calling of PHP native functions, so we can properly test the code
  334. if (!defined('ADODB_TEST_DATES')) define('ADODB_TEST_DATES',1);
  335. $t = time();
  336. $fmt = 'Y-m-d H:i:s';
  337. echo '<pre>';
  338. echo 'adodb: ',adodb_date($fmt,$t),'<br>';
  339. echo 'php : ',date($fmt,$t),'<br>';
  340. echo '</pre>';
  341. adodb_date_test_strftime('%Y %m %x %X');
  342. adodb_date_test_strftime("%A %d %B %Y");
  343. adodb_date_test_strftime("%H %M S");
  344. $t = adodb_mktime(0,0,0);
  345. if (!(adodb_date('Y-m-d') == date('Y-m-d'))) print 'Error in '.adodb_mktime(0,0,0).'<br>';
  346. $t = adodb_mktime(0,0,0,6,1,2102);
  347. if (!(adodb_date('Y-m-d',$t) == '2102-06-01')) print 'Error in '.adodb_date('Y-m-d',$t).'<br>';
  348. $t = adodb_mktime(0,0,0,2,1,2102);
  349. if (!(adodb_date('Y-m-d',$t) == '2102-02-01')) print 'Error in '.adodb_date('Y-m-d',$t).'<br>';
  350. print "<p>Testing gregorian <=> julian conversion<p>";
  351. $t = adodb_mktime(0,0,0,10,11,1492);
  352. //http://www.holidayorigins.com/html/columbus_day.html - Friday check
  353. if (!(adodb_date('D Y-m-d',$t) == 'Fri 1492-10-11')) print 'Error in Columbus landing<br>';
  354. $t = adodb_mktime(0,0,0,2,29,1500);
  355. if (!(adodb_date('Y-m-d',$t) == '1500-02-29')) print 'Error in julian leap years<br>';
  356. $t = adodb_mktime(0,0,0,2,29,1700);
  357. if (!(adodb_date('Y-m-d',$t) == '1700-03-01')) print 'Error in gregorian leap years<br>';
  358. print adodb_mktime(0,0,0,10,4,1582).' ';
  359. print adodb_mktime(0,0,0,10,15,1582);
  360. $diff = (adodb_mktime(0,0,0,10,15,1582) - adodb_mktime(0,0,0,10,4,1582));
  361. if ($diff != 3600*24) print " <b>Error in gregorian correction = ".($diff/3600/24)." days </b><br>";
  362. print " 15 Oct 1582, Fri=".(adodb_dow(1582,10,15) == 5 ? 'Fri' : '<b>Error</b>')."<br>";
  363. print " 4 Oct 1582, Thu=".(adodb_dow(1582,10,4) == 4 ? 'Thu' : '<b>Error</b>')."<br>";
  364. print "<p>Testing overflow<p>";
  365. $t = adodb_mktime(0,0,0,3,33,1965);
  366. if (!(adodb_date('Y-m-d',$t) == '1965-04-02')) print 'Error in day overflow 1 <br>';
  367. $t = adodb_mktime(0,0,0,4,33,1971);
  368. if (!(adodb_date('Y-m-d',$t) == '1971-05-03')) print 'Error in day overflow 2 <br>';
  369. $t = adodb_mktime(0,0,0,1,60,1965);
  370. if (!(adodb_date('Y-m-d',$t) == '1965-03-01')) print 'Error in day overflow 3 '.adodb_date('Y-m-d',$t).' <br>';
  371. $t = adodb_mktime(0,0,0,12,32,1965);
  372. if (!(adodb_date('Y-m-d',$t) == '1966-01-01')) print 'Error in day overflow 4 '.adodb_date('Y-m-d',$t).' <br>';
  373. $t = adodb_mktime(0,0,0,12,63,1965);
  374. if (!(adodb_date('Y-m-d',$t) == '1966-02-01')) print 'Error in day overflow 5 '.adodb_date('Y-m-d',$t).' <br>';
  375. $t = adodb_mktime(0,0,0,13,3,1965);
  376. if (!(adodb_date('Y-m-d',$t) == '1966-01-03')) print 'Error in mth overflow 1 <br>';
  377. print "Testing 2-digit => 4-digit year conversion<p>";
  378. if (adodb_year_digit_check(00) != 2000) print "Err 2-digit 2000<br>";
  379. if (adodb_year_digit_check(10) != 2010) print "Err 2-digit 2010<br>";
  380. if (adodb_year_digit_check(20) != 2020) print "Err 2-digit 2020<br>";
  381. if (adodb_year_digit_check(30) != 2030) print "Err 2-digit 2030<br>";
  382. if (adodb_year_digit_check(40) != 1940) print "Err 2-digit 1940<br>";
  383. if (adodb_year_digit_check(50) != 1950) print "Err 2-digit 1950<br>";
  384. if (adodb_year_digit_check(90) != 1990) print "Err 2-digit 1990<br>";
  385. // Test string formating
  386. print "<p>Testing date formating</p>";
  387. $fmt = '\d\a\t\e T Y-m-d H:i:s a A d D F g G h H i j l L m M n O \R\F\C2822 r s t U w y Y z Z 2003';
  388. $s1 = date($fmt,0);
  389. $s2 = adodb_date($fmt,0);
  390. if ($s1 != $s2) {
  391. print " date() 0 failed<br>$s1<br>$s2<br>";
  392. }
  393. flush();
  394. for ($i=100; --$i > 0; ) {
  395. $ts = 3600.0*((rand()%60000)+(rand()%60000))+(rand()%60000);
  396. $s1 = date($fmt,$ts);
  397. $s2 = adodb_date($fmt,$ts);
  398. //print "$s1 <br>$s2 <p>";
  399. $pos = strcmp($s1,$s2);
  400. if (($s1) != ($s2)) {
  401. for ($j=0,$k=strlen($s1); $j < $k; $j++) {
  402. if ($s1[$j] != $s2[$j]) {
  403. print substr($s1,$j).' ';
  404. break;
  405. }
  406. }
  407. print "<b>Error date(): $ts<br><pre>
  408. &nbsp; \"$s1\" (date len=".strlen($s1).")
  409. &nbsp; \"$s2\" (adodb_date len=".strlen($s2).")</b></pre><br>";
  410. $fail = true;
  411. }
  412. $a1 = getdate($ts);
  413. $a2 = adodb_getdate($ts);
  414. $rez = array_diff($a1,$a2);
  415. if (sizeof($rez)>0) {
  416. print "<b>Error getdate() $ts</b><br>";
  417. print_r($a1);
  418. print "<br>";
  419. print_r($a2);
  420. print "<p>";
  421. $fail = true;
  422. }
  423. }
  424. // Test generation of dates outside 1901-2038
  425. print "<p>Testing random dates between 100 and 4000</p>";
  426. adodb_date_test_date(100,1);
  427. for ($i=100; --$i >= 0;) {
  428. $y1 = 100+rand(0,1970-100);
  429. $m = rand(1,12);
  430. adodb_date_test_date($y1,$m);
  431. $y1 = 3000-rand(0,3000-1970);
  432. adodb_date_test_date($y1,$m);
  433. }
  434. print '<p>';
  435. $start = 1960+rand(0,10);
  436. $yrs = 12;
  437. $i = 365.25*86400*($start-1970);
  438. $offset = 36000+rand(10000,60000);
  439. $max = 365*$yrs*86400;
  440. $lastyear = 0;
  441. // we generate a timestamp, convert it to a date, and convert it back to a timestamp
  442. // and check if the roundtrip broke the original timestamp value.
  443. print "Testing $start to ".($start+$yrs).", or $max seconds, offset=$offset: ";
  444. $cnt = 0;
  445. for ($max += $i; $i < $max; $i += $offset) {
  446. $ret = adodb_date('m,d,Y,H,i,s',$i);
  447. $arr = explode(',',$ret);
  448. if ($lastyear != $arr[2]) {
  449. $lastyear = $arr[2];
  450. print " $lastyear ";
  451. flush();
  452. }
  453. $newi = adodb_mktime($arr[3],$arr[4],$arr[5],$arr[0],$arr[1],$arr[2]);
  454. if ($i != $newi) {
  455. print "Error at $i, adodb_mktime returned $newi ($ret)";
  456. $fail = true;
  457. break;
  458. }
  459. $cnt += 1;
  460. }
  461. echo "Tested $cnt dates<br>";
  462. if (!$fail) print "<p>Passed !</p>";
  463. else print "<p><b>Failed</b> :-(</p>";
  464. }
  465. /**
  466. Returns day of week, 0 = Sunday,... 6=Saturday.
  467. Algorithm from PEAR::Date_Calc
  468. */
  469. function adodb_dow($year, $month, $day)
  470. {
  471. /*
  472. Pope Gregory removed 10 days - October 5 to October 14 - from the year 1582 and
  473. proclaimed that from that time onwards 3 days would be dropped from the calendar
  474. every 400 years.
  475. Thursday, October 4, 1582 (Julian) was followed immediately by Friday, October 15, 1582 (Gregorian).
  476. */
  477. if ($year <= 1582) {
  478. if ($year < 1582 ||
  479. ($year == 1582 && ($month < 10 || ($month == 10 && $day < 15)))) $greg_correction = 3;
  480. else
  481. $greg_correction = 0;
  482. } else
  483. $greg_correction = 0;
  484. if($month > 2)
  485. $month -= 2;
  486. else {
  487. $month += 10;
  488. $year--;
  489. }
  490. $day = floor((13 * $month - 1) / 5) +
  491. $day + ($year % 100) +
  492. floor(($year % 100) / 4) +
  493. floor(($year / 100) / 4) - 2 *
  494. floor($year / 100) + 77 + $greg_correction;
  495. return $day - 7 * floor($day / 7);
  496. }
  497. /**
  498. Checks for leap year, returns true if it is. No 2-digit year check. Also
  499. handles julian calendar correctly.
  500. */
  501. function _adodb_is_leap_year($year)
  502. {
  503. if ($year % 4 != 0) return false;
  504. if ($year % 400 == 0) {
  505. return true;
  506. // if gregorian calendar (>1582), century not-divisible by 400 is not leap
  507. } else if ($year > 1582 && $year % 100 == 0 ) {
  508. return false;
  509. }
  510. return true;
  511. }
  512. /**
  513. checks for leap year, returns true if it is. Has 2-digit year check
  514. */
  515. function adodb_is_leap_year($year)
  516. {
  517. return _adodb_is_leap_year(adodb_year_digit_check($year));
  518. }
  519. /**
  520. Fix 2-digit years. Works for any century.
  521. Assumes that if 2-digit is more than 30 years in future, then previous century.
  522. */
  523. function adodb_year_digit_check($y)
  524. {
  525. if ($y < 100) {
  526. $yr = (integer) date("Y");
  527. $century = (integer) ($yr /100);
  528. if ($yr%100 > 50) {
  529. $c1 = $century + 1;
  530. $c0 = $century;
  531. } else {
  532. $c1 = $century;
  533. $c0 = $century - 1;
  534. }
  535. $c1 *= 100;
  536. // if 2-digit year is less than 30 years in future, set it to this century
  537. // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
  538. if (($y + $c1) < $yr+30) $y = $y + $c1;
  539. else $y = $y + $c0*100;
  540. }
  541. return $y;
  542. }
  543. function adodb_get_gmt_diff_ts($ts)
  544. {
  545. if (0 <= $ts && $ts <= 0x7FFFFFFF) { // check if number in 32-bit signed range) {
  546. $arr = getdate($ts);
  547. $y = $arr['year'];
  548. $m = $arr['mon'];
  549. $d = $arr['mday'];
  550. return adodb_get_gmt_diff($y,$m,$d);
  551. } else {
  552. return adodb_get_gmt_diff(false,false,false);
  553. }
  554. }
  555. /**
  556. get local time zone offset from GMT. Does not handle historical timezones before 1970.
  557. */
  558. function adodb_get_gmt_diff($y,$m,$d)
  559. {
  560. static $TZ,$tzo;
  561. global $ADODB_DATETIME_CLASS;
  562. if (!defined('ADODB_TEST_DATES')) $y = false;
  563. else if ($y < 1970 || $y >= 2038) $y = false;
  564. if ($ADODB_DATETIME_CLASS && $y !== false) {
  565. $dt = new DateTime();
  566. $dt->setISODate($y,$m,$d);
  567. if (empty($tzo)) {
  568. $tzo = new DateTimeZone(date_default_timezone_get());
  569. # $tzt = timezone_transitions_get( $tzo );
  570. }
  571. return -$tzo->getOffset($dt);
  572. } else {
  573. if (isset($TZ)) return $TZ;
  574. $y = date('Y');
  575. $TZ = mktime(0,0,0,12,2,$y,0) - gmmktime(0,0,0,12,2,$y,0);
  576. }
  577. return $TZ;
  578. }
  579. /**
  580. Returns an array with date info.
  581. */
  582. function adodb_getdate($d=false,$fast=false)
  583. {
  584. if ($d === false) return getdate();
  585. if (!defined('ADODB_TEST_DATES')) {
  586. if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
  587. if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer
  588. return @getdate($d);
  589. }
  590. }
  591. return _adodb_getdate($d);
  592. }
  593. /*
  594. // generate $YRS table for _adodb_getdate()
  595. function adodb_date_gentable($out=true)
  596. {
  597. for ($i=1970; $i >= 1600; $i-=10) {
  598. $s = adodb_gmmktime(0,0,0,1,1,$i);
  599. echo "$i => $s,<br>";
  600. }
  601. }
  602. adodb_date_gentable();
  603. for ($i=1970; $i > 1500; $i--) {
  604. echo "<hr />$i ";
  605. adodb_date_test_date($i,1,1);
  606. }
  607. */
  608. $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
  609. $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
  610. function adodb_validdate($y,$m,$d)
  611. {
  612. global $_month_table_normal,$_month_table_leaf;
  613. if (_adodb_is_leap_year($y)) $marr = $_month_table_leaf;
  614. else $marr = $_month_table_normal;
  615. if ($m > 12 || $m < 1) return false;
  616. if ($d > 31 || $d < 1) return false;
  617. if ($marr[$m] < $d) return false;
  618. if ($y < 1000 && $y > 3000) return false;
  619. return true;
  620. }
  621. /**
  622. Low-level function that returns the getdate() array. We have a special
  623. $fast flag, which if set to true, will return fewer array values,
  624. and is much faster as it does not calculate dow, etc.
  625. */
  626. function _adodb_getdate($origd=false,$fast=false,$is_gmt=false)
  627. {
  628. static $YRS;
  629. global $_month_table_normal,$_month_table_leaf;
  630. $d = $origd - ($is_gmt ? 0 : adodb_get_gmt_diff_ts($origd));
  631. $_day_power = 86400;
  632. $_hour_power = 3600;
  633. $_min_power = 60;
  634. if ($d < -12219321600) $d -= 86400*10; // if 15 Oct 1582 or earlier, gregorian correction
  635. $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
  636. $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
  637. $d366 = $_day_power * 366;
  638. $d365 = $_day_power * 365;
  639. if ($d < 0) {
  640. if (empty($YRS)) $YRS = array(
  641. 1970 => 0,
  642. 1960 => -315619200,
  643. 1950 => -631152000,
  644. 1940 => -946771200,
  645. 1930 => -1262304000,
  646. 1920 => -1577923200,
  647. 1910 => -1893456000,
  648. 1900 => -2208988800,
  649. 1890 => -2524521600,
  650. 1880 => -2840140800,
  651. 1870 => -3155673600,
  652. 1860 => -3471292800,
  653. 1850 => -3786825600,
  654. 1840 => -4102444800,
  655. 1830 => -4417977600,
  656. 1820 => -4733596800,
  657. 1810 => -5049129600,
  658. 1800 => -5364662400,
  659. 1790 => -5680195200,
  660. 1780 => -5995814400,
  661. 1770 => -6311347200,
  662. 1760 => -6626966400,
  663. 1750 => -6942499200,
  664. 1740 => -7258118400,
  665. 1730 => -7573651200,
  666. 1720 => -7889270400,
  667. 1710 => -8204803200,
  668. 1700 => -8520336000,
  669. 1690 => -8835868800,
  670. 1680 => -9151488000,
  671. 1670 => -9467020800,
  672. 1660 => -9782640000,
  673. 1650 => -10098172800,
  674. 1640 => -10413792000,
  675. 1630 => -10729324800,
  676. 1620 => -11044944000,
  677. 1610 => -11360476800,
  678. 1600 => -11676096000);
  679. if ($is_gmt) $origd = $d;
  680. // The valid range of a 32bit signed timestamp is typically from
  681. // Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT
  682. //
  683. # old algorithm iterates through all years. new algorithm does it in
  684. # 10 year blocks
  685. /*
  686. # old algo
  687. for ($a = 1970 ; --$a >= 0;) {
  688. $lastd = $d;
  689. if ($leaf = _adodb_is_leap_year($a)) $d += $d366;
  690. else $d += $d365;
  691. if ($d >= 0) {
  692. $year = $a;
  693. break;
  694. }
  695. }
  696. */
  697. $lastsecs = 0;
  698. $lastyear = 1970;
  699. foreach($YRS as $year => $secs) {
  700. if ($d >= $secs) {
  701. $a = $lastyear;
  702. break;
  703. }
  704. $lastsecs = $secs;
  705. $lastyear = $year;
  706. }
  707. $d -= $lastsecs;
  708. if (!isset($a)) $a = $lastyear;
  709. //echo ' yr=',$a,' ', $d,'.';
  710. for (; --$a >= 0;) {
  711. $lastd = $d;
  712. if ($leaf = _adodb_is_leap_year($a)) $d += $d366;
  713. else $d += $d365;
  714. if ($d >= 0) {
  715. $year = $a;
  716. break;
  717. }
  718. }
  719. /**/
  720. $secsInYear = 86400 * ($leaf ? 366 : 365) + $lastd;
  721. $d = $lastd;
  722. $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal;
  723. for ($a = 13 ; --$a > 0;) {
  724. $lastd = $d;
  725. $d += $mtab[$a] * $_day_power;
  726. if ($d >= 0) {
  727. $month = $a;
  728. $ndays = $mtab[$a];
  729. break;
  730. }
  731. }
  732. $d = $lastd;
  733. $day = $ndays + ceil(($d+1) / ($_day_power));
  734. $d += ($ndays - $day+1)* $_day_power;
  735. $hour = floor($d/$_hour_power);
  736. } else {
  737. for ($a = 1970 ;; $a++) {
  738. $lastd = $d;
  739. if ($leaf = _adodb_is_leap_year($a)) $d -= $d366;
  740. else $d -= $d365;
  741. if ($d < 0) {
  742. $year = $a;
  743. break;
  744. }
  745. }
  746. $secsInYear = $lastd;
  747. $d = $lastd;
  748. $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal;
  749. for ($a = 1 ; $a <= 12; $a++) {
  750. $lastd = $d;
  751. $d -= $mtab[$a] * $_day_power;
  752. if ($d < 0) {
  753. $month = $a;
  754. $ndays = $mtab[$a];
  755. break;
  756. }
  757. }
  758. $d = $lastd;
  759. $day = ceil(($d+1) / $_day_power);
  760. $d = $d - ($day-1) * $_day_power;
  761. $hour = floor($d /$_hour_power);
  762. }
  763. $d -= $hour * $_hour_power;
  764. $min = floor($d/$_min_power);
  765. $secs = $d - $min * $_min_power;
  766. if ($fast) {
  767. return array(
  768. 'seconds' => $secs,
  769. 'minutes' => $min,
  770. 'hours' => $hour,
  771. 'mday' => $day,
  772. 'mon' => $month,
  773. 'year' => $year,
  774. 'yday' => floor($secsInYear/$_day_power),
  775. 'leap' => $leaf,
  776. 'ndays' => $ndays
  777. );
  778. }
  779. $dow = adodb_dow($year,$month,$day);
  780. return array(
  781. 'seconds' => $secs,
  782. 'minutes' => $min,
  783. 'hours' => $hour,
  784. 'mday' => $day,
  785. 'wday' => $dow,
  786. 'mon' => $month,
  787. 'year' => $year,
  788. 'yday' => floor($secsInYear/$_day_power),
  789. 'weekday' => gmdate('l',$_day_power*(3+$dow)),
  790. 'month' => gmdate('F',mktime(0,0,0,$month,2,1971)),
  791. 0 => $origd
  792. );
  793. }
  794. /*
  795. if ($isphp5)
  796. $dates .= sprintf('%s%04d',($gmt<=0)?'+':'-',abs($gmt)/36);
  797. else
  798. $dates .= sprintf('%s%04d',($gmt<0)?'+':'-',abs($gmt)/36);
  799. break;*/
  800. function adodb_tz_offset($gmt,$isphp5)
  801. {
  802. $zhrs = abs($gmt)/3600;
  803. $hrs = floor($zhrs);
  804. if ($isphp5)
  805. return sprintf('%s%02d%02d',($gmt<=0)?'+':'-',floor($zhrs),($zhrs-$hrs)*60);
  806. else
  807. return sprintf('%s%02d%02d',($gmt<0)?'+':'-',floor($zhrs),($zhrs-$hrs)*60);
  808. break;
  809. }
  810. function adodb_gmdate($fmt,$d=false)
  811. {
  812. return adodb_date($fmt,$d,true);
  813. }
  814. // accepts unix timestamp and iso date format in $d
  815. function adodb_date2($fmt, $d=false, $is_gmt=false)
  816. {
  817. if ($d !== false) {
  818. if (!preg_match(
  819. "|^([0-9]{4})[-/\.]?([0-9]{1,2})[-/\.]?([0-9]{1,2})[ -]?(([0-9]{1,2}):?([0-9]{1,2}):?([0-9\.]{1,4}))?|",
  820. ($d), $rr)) return adodb_date($fmt,false,$is_gmt);
  821. if ($rr[1] <= 100 && $rr[2]<= 1) return adodb_date($fmt,false,$is_gmt);
  822. // h-m-s-MM-DD-YY
  823. if (!isset($rr[5])) $d = adodb_mktime(0,0,0,$rr[2],$rr[3],$rr[1],false,$is_gmt);
  824. else $d = @adodb_mktime($rr[5],$rr[6],$rr[7],$rr[2],$rr[3],$rr[1],false,$is_gmt);
  825. }
  826. return adodb_date($fmt,$d,$is_gmt);
  827. }
  828. /**
  829. Return formatted date based on timestamp $d
  830. */
  831. function adodb_date($fmt,$d=false,$is_gmt=false)
  832. {
  833. static $daylight;
  834. global $ADODB_DATETIME_CLASS;
  835. if ($d === false) return ($is_gmt)? @gmdate($fmt): @date($fmt);
  836. if (!defined('ADODB_TEST_DATES')) {
  837. if ((abs($d) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
  838. if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer
  839. return ($is_gmt)? @gmdate($fmt,$d): @date($fmt,$d);
  840. }
  841. }
  842. $_day_power = 86400;
  843. $arr = _adodb_getdate($d,true,$is_gmt);
  844. if (!isset($daylight)) $daylight = function_exists('adodb_daylight_sv');
  845. if ($daylight) adodb_daylight_sv($arr, $is_gmt);
  846. $year = $arr['year'];
  847. $month = $arr['mon'];
  848. $day = $arr['mday'];
  849. $hour = $arr['hours'];
  850. $min = $arr['minutes'];
  851. $secs = $arr['seconds'];
  852. $max = strlen($fmt);
  853. $dates = '';
  854. $isphp5 = PHP_VERSION >= 5;
  855. /*
  856. at this point, we have the following integer vars to manipulate:
  857. $year, $month, $day, $hour, $min, $secs
  858. */
  859. for ($i=0; $i < $max; $i++) {
  860. switch($fmt[$i]) {
  861. case 'T':
  862. if ($ADODB_DATETIME_CLASS) {
  863. $dt = new DateTime();
  864. $dt->SetDate($year,$month,$day);
  865. $dates .= $dt->Format('T');
  866. } else
  867. $dates .= date('T');
  868. break;
  869. // YEAR
  870. case 'L': $dates .= $arr['leap'] ? '1' : '0'; break;
  871. case 'r': // Thu, 21 Dec 2000 16:01:07 +0200
  872. // 4.3.11 uses '04 Jun 2004'
  873. // 4.3.8 uses ' 4 Jun 2004'
  874. $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))).', '
  875. . ($day<10?'0'.$day:$day) . ' '.date('M',mktime(0,0,0,$month,2,1971)).' '.$year.' ';
  876. if ($hour < 10) $dates .= '0'.$hour; else $dates .= $hour;
  877. if ($min < 10) $dates .= ':0'.$min; else $dates .= ':'.$min;
  878. if ($secs < 10) $dates .= ':0'.$secs; else $dates .= ':'.$secs;
  879. $gmt = adodb_get_gmt_diff($year,$month,$day);
  880. $dates .= ' '.adodb_tz_offset($gmt,$isphp5);
  881. break;
  882. case 'Y': $dates .= $year; break;
  883. case 'y': $dates .= substr($year,strlen($year)-2,2); break;
  884. // MONTH
  885. case 'm': if ($month<10) $dates .= '0'.$month; else $dates .= $month; break;
  886. case 'Q': $dates .= ($month+3)>>2; break;
  887. case 'n': $dates .= $month; break;
  888. case 'M': $dates .= date('M',mktime(0,0,0,$month,2,1971)); break;
  889. case 'F': $dates .= date('F',mktime(0,0,0,$month,2,1971)); break;
  890. // DAY
  891. case 't': $dates .= $arr['ndays']; break;
  892. case 'z': $dates .= $arr['yday']; break;
  893. case 'w': $dates .= adodb_dow($year,$month,$day); break;
  894. case 'l': $dates .= gmdate('l',$_day_power*(3+adodb_dow($year,$month,$day))); break;
  895. case 'D': $dates .= gmdate('D',$_day_power*(3+adodb_dow($year,$month,$day))); break;
  896. case 'j': $dates .= $day; break;
  897. case 'd': if ($day<10) $dates .= '0'.$day; else $dates .= $day; break;
  898. case 'S':
  899. $d10 = $day % 10;
  900. if ($d10 == 1) $dates .= 'st';
  901. else if ($d10 == 2 && $day != 12) $dates .= 'nd';
  902. else if ($d10 == 3) $dates .= 'rd';
  903. else $dates .= 'th';
  904. break;
  905. // HOUR
  906. case 'Z':
  907. $dates .= ($is_gmt) ? 0 : -adodb_get_gmt_diff($year,$month,$day); break;
  908. case 'O':
  909. $gmt = ($is_gmt) ? 0 : adodb_get_gmt_diff($year,$month,$day);
  910. $dates .= adodb_tz_offset($gmt,$isphp5);
  911. break;
  912. case 'H':
  913. if ($hour < 10) $dates .= '0'.$hour;
  914. else $dates .= $hour;
  915. break;
  916. case 'h':
  917. if ($hour > 12) $hh = $hour - 12;
  918. else {
  919. if ($hour == 0) $hh = '12';
  920. else $hh = $hour;
  921. }
  922. if ($hh < 10) $dates .= '0'.$hh;
  923. else $dates .= $hh;
  924. break;
  925. case 'G':
  926. $dates .= $hour;
  927. break;
  928. case 'g':
  929. if ($hour > 12) $hh = $hour - 12;
  930. else {
  931. if ($hour == 0) $hh = '12';
  932. else $hh = $hour;
  933. }
  934. $dates .= $hh;
  935. break;
  936. // MINUTES
  937. case 'i': if ($min < 10) $dates .= '0'.$min; else $dates .= $min; break;
  938. // SECONDS
  939. case 'U': $dates .= $d; break;
  940. case 's': if ($secs < 10) $dates .= '0'.$secs; else $dates .= $secs; break;
  941. // AM/PM
  942. // Note 00:00 to 11:59 is AM, while 12:00 to 23:59 is PM
  943. case 'a':
  944. if ($hour>=12) $dates .= 'pm';
  945. else $dates .= 'am';
  946. break;
  947. case 'A':
  948. if ($hour>=12) $dates .= 'PM';
  949. else $dates .= 'AM';
  950. break;
  951. default:
  952. $dates .= $fmt[$i]; break;
  953. // ESCAPE
  954. case "\\":
  955. $i++;
  956. if ($i < $max) $dates .= $fmt[$i];
  957. break;
  958. }
  959. }
  960. return $dates;
  961. }
  962. /**
  963. Returns a timestamp given a GMT/UTC time.
  964. Note that $is_dst is not implemented and is ignored.
  965. */
  966. function adodb_gmmktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false)
  967. {
  968. return adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,true);
  969. }
  970. /**
  971. Return a timestamp given a local time. Originally by jackbbs.
  972. Note that $is_dst is not implemented and is ignored.
  973. Not a very fast algorithm - O(n) operation. Could be optimized to O(1).
  974. */
  975. function adodb_mktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false,$is_gmt=false)
  976. {
  977. if (!defined('ADODB_TEST_DATES')) {
  978. if ($mon === false) {
  979. return $is_gmt? @gmmktime($hr,$min,$sec): @mktime($hr,$min,$sec);
  980. }
  981. // for windows, we don't check 1970 because with timezone differences,
  982. // 1 Jan 1970 could generate negative timestamp, which is illegal
  983. $usephpfns = (1971 < $year && $year < 2038
  984. || !defined('ADODB_NO_NEGATIVE_TS') && (1901 < $year && $year < 2038)
  985. );
  986. if ($usephpfns && ($year + $mon/12+$day/365.25+$hr/(24*365.25) >= 2038)) $usephpfns = false;
  987. if ($usephpfns) {
  988. return $is_gmt ?
  989. @gmmktime($hr,$min,$sec,$mon,$day,$year):
  990. @mktime($hr,$min,$sec,$mon,$day,$year);
  991. }
  992. }
  993. $gmt_different = ($is_gmt) ? 0 : adodb_get_gmt_diff($year,$mon,$day);
  994. /*
  995. # disabled because some people place large values in $sec.
  996. # however we need it for $mon because we use an array...
  997. $hr = intval($hr);
  998. $min = intval($min);
  999. $sec = intval($sec);
  1000. */
  1001. $mon = intval($mon);
  1002. $day = intval($day);
  1003. $year = intval($year);
  1004. $year = adodb_year_digit_check($year);
  1005. if ($mon > 12) {
  1006. $y = floor(($mon-1)/ 12);
  1007. $year += $y;
  1008. $mon -= $y*12;
  1009. } else if ($mon < 1) {
  1010. $y = ceil((1-$mon) / 12);
  1011. $year -= $y;
  1012. $mon += $y*12;
  1013. }
  1014. $_day_power = 86400;
  1015. $_hour_power = 3600;
  1016. $_min_power = 60;
  1017. $_month_table_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
  1018. $_month_table_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
  1019. $_total_date = 0;
  1020. if ($year >= 1970) {
  1021. for ($a = 1970 ; $a <= $year; $a++) {
  1022. $leaf = _adodb_is_leap_year($a);
  1023. if ($leaf == true) {
  1024. $loop_table = $_month_table_leaf;
  1025. $_add_date = 366;
  1026. } else {
  1027. $loop_table = $_month_table_normal;
  1028. $_add_date = 365;
  1029. }
  1030. if ($a < $year) {
  1031. $_total_date += $_add_date;
  1032. } else {
  1033. for($b=1;$b<$mon;$b++) {
  1034. $_total_date += $loop_table[$b];
  1035. }
  1036. }
  1037. }
  1038. $_total_date +=$day-1;
  1039. $ret = $_total_date * $_day_power + $hr * $_hour_power + $min * $_min_power + $sec + $gmt_different;
  1040. } else {
  1041. for ($a = 1969 ; $a >= $year; $a--) {
  1042. $leaf = _adodb_is_leap_year($a);
  1043. if ($leaf == true) {
  1044. $loop_table = $_month_table_leaf;
  1045. $_add_date = 366;
  1046. } else {
  1047. $loop_table = $_month_table_normal;
  1048. $_add_date = 365;
  1049. }
  1050. if ($a > $year) { $_total_date += $_add_date;
  1051. } else {
  1052. for($b=12;$b>$mon;$b--) {
  1053. $_total_date += $loop_table[$b];
  1054. }
  1055. }
  1056. }
  1057. $_total_date += $loop_table[$mon] - $day;
  1058. $_day_time = $hr * $_hour_power + $min * $_min_power + $sec;
  1059. $_day_time = $_day_power - $_day_time;
  1060. $ret = -( $_total_date * $_day_power + $_day_time - $gmt_different);
  1061. if ($ret < -12220185600) $ret += 10*86400; // if earlier than 5 Oct 1582 - gregorian correction
  1062. else if ($ret < -12219321600) $ret = -12219321600; // if in limbo, reset to 15 Oct 1582.
  1063. }
  1064. //print " dmy=$day/$mon/$year $hr:$min:$sec => " .$ret;
  1065. return $ret;
  1066. }
  1067. function adodb_gmstrftime($fmt, $ts=false)
  1068. {
  1069. return adodb_strftime($fmt,$ts,true);
  1070. }
  1071. // hack - convert to adodb_date
  1072. function adodb_strftime($fmt, $ts=false,$is_gmt=false)
  1073. {
  1074. global $ADODB_DATE_LOCALE;
  1075. if (!defined('ADODB_TEST_DATES')) {
  1076. if ((abs($ts) <= 0x7FFFFFFF)) { // check if number in 32-bit signed range
  1077. if (!defined('ADODB_NO_NEGATIVE_TS') || $ts >= 0) // if windows, must be +ve integer
  1078. return ($is_gmt)? @gmstrftime($fmt,$ts): @strftime($fmt,$ts);
  1079. }
  1080. }
  1081. if (empty($ADODB_DATE_LOCALE)) {
  1082. /*
  1083. $tstr = strtoupper(gmstrftime('%c',31366800)); // 30 Dec 1970, 1 am
  1084. $sep = substr($tstr,2,1);
  1085. $hasAM = strrpos($tstr,'M') !== false;
  1086. */
  1087. # see http://phplens.com/lens/lensforum/msgs.php?id=14865 for reasoning, and changelog for version 0.24
  1088. $dstr = gmstrftime('%x',31366800); // 30 Dec 1970, 1 am
  1089. $sep = substr($dstr,2,1);
  1090. $tstr = strtoupper(gmstrftime('%X',31366800)); // 30 Dec 1970, 1 am
  1091. $hasAM = strrpos($tstr,'M') !== false;
  1092. $ADODB_DATE_LOCALE = array();
  1093. $ADODB_DATE_LOCALE[] = strncmp($tstr,'30',2) == 0 ? 'd'.$sep.'m'.$sep.'y' : 'm'.$sep.'d'.$sep.'y';
  1094. $ADODB_DATE_LOCALE[] = ($hasAM) ? 'h:i:s a' : 'H:i:s';
  1095. }
  1096. $inpct = false;
  1097. $fmtdate = '';
  1098. for ($i=0,$max = strlen($fmt); $i < $max; $i++) {
  1099. $ch = $fmt[$i];
  1100. if ($ch == '%') {
  1101. if ($inpct) {
  1102. $fmtdate .= '%';
  1103. $inpct = false;
  1104. } else
  1105. $inpct = true;
  1106. } else if ($inpct) {
  1107. $inpct = false;
  1108. switch($ch) {
  1109. case '0':
  1110. case '1':
  1111. case '2':
  1112. case '3':
  1113. case '4':
  1114. case '5':
  1115. case '6':
  1116. case '7':
  1117. case '8':
  1118. case '9':
  1119. case 'E':
  1120. case 'O':
  1121. /* ignore format modifiers */
  1122. $inpct = true;
  1123. break;
  1124. case 'a': $fmtdate .= 'D'; break;
  1125. case 'A': $fmtdate .= 'l'; break;
  1126. case 'h':
  1127. case 'b': $fmtdate .= 'M'; break;
  1128. case 'B': $fmtdate .= 'F'; break;
  1129. case 'c': $fmtdate .= $ADODB_DATE_LOCALE[0].$ADODB_DATE_LOCALE[1]; break;
  1130. case 'C': $fmtdate .= '\C?'; break; // century
  1131. case 'd': $fmtdate .= 'd'; break;
  1132. case 'D': $fmtdate .= 'm/d/y'; break;
  1133. case 'e': $fmtdate .= 'j'; break;
  1134. case 'g': $fmtdate .= '\g?'; break; //?
  1135. case 'G': $fmtdate .= '\G?'; break; //?
  1136. case 'H': $fmtdate .= 'H'; break;
  1137. case 'I': $fmtdate .= 'h'; break;
  1138. case 'j': $fmtdate .= '?z'; $parsej = true; break; // wrong as j=1-based, z=0-basd
  1139. case 'm': $fmtdate .= 'm'; break;
  1140. case 'M': $fmtdate .= 'i'; break;
  1141. case 'n': $fmtdate .= "\n"; break;
  1142. case 'p': $fmtdate .= 'a'; break;
  1143. case 'r': $fmtdate .= 'h:i:s a'; break;
  1144. case 'R': $fmtdate .= 'H:i:s'; break;
  1145. case 'S': $fmtdate .= 's'; break;
  1146. case 't': $fmtdate .= "\t"; break;
  1147. case 'T': $fmtdate .= 'H:i:s'; break;
  1148. case 'u': $fmtdate .= '?u'; $parseu = true; break; // wrong strftime=1-based, date=0-based
  1149. case 'U': $fmtdate .= '?U'; $parseU = true; break;// wrong strftime=1-based, date=0-based
  1150. case 'x': $fmtdate .= $ADODB_DATE_LOCALE[0]; break;
  1151. case 'X': $fmtdate .= $ADODB_DATE_LOCALE[1]; break;
  1152. case 'w': $fmtdate .= '?w'; $parseu = true; break; // wrong strftime=1-based, date=0-based
  1153. case 'W': $fmtdate .= '?W'; $parseU = true; break;// wrong strftime=1-based, date=0-based
  1154. case 'y': $fmtdate .= 'y'; break;
  1155. case 'Y': $fmtdate .= 'Y'; break;
  1156. case 'Z': $fmtdate .= 'T'; break;
  1157. }
  1158. } else if (('A' <= ($ch) && ($ch) <= 'Z' ) || ('a' <= ($ch) && ($ch) <= 'z' ))
  1159. $fmtdate .= "\\".$ch;
  1160. else
  1161. $fmtdate .= $ch;
  1162. }
  1163. //echo "fmt=",$fmtdate,"<br>";
  1164. if ($ts === false) $ts = time();
  1165. $ret = adodb_date($fmtdate, $ts, $is_gmt);
  1166. return $ret;
  1167. }
  1168. ?>