An PHP based Image Database
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
9.6 KiB

  1. function date (format, timestamp) {
  2. // http://kevin.vanzonneveld.net
  3. // + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
  4. // + parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html)
  5. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  6. // + improved by: MeEtc (http://yass.meetcweb.com)
  7. // + improved by: Brad Touesnard
  8. // + improved by: Tim Wiel
  9. // + improved by: Bryan Elliott
  10. //
  11. // + improved by: Brett Zamir (http://brett-zamir.me)
  12. // + improved by: David Randall
  13. // + input by: Brett Zamir (http://brett-zamir.me)
  14. // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  15. // + improved by: Brett Zamir (http://brett-zamir.me)
  16. // + improved by: Brett Zamir (http://brett-zamir.me)
  17. // + improved by: Theriault
  18. // + derived from: gettimeofday
  19. // + input by: majak
  20. // + bugfixed by: majak
  21. // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  22. // + input by: Alex
  23. // + bugfixed by: Brett Zamir (http://brett-zamir.me)
  24. // + improved by: Theriault
  25. // + improved by: Brett Zamir (http://brett-zamir.me)
  26. // + improved by: Theriault
  27. // + improved by: Thomas Beaucourt (http://www.webapp.fr)
  28. // + improved by: JT
  29. // + improved by: Theriault
  30. // + improved by: Rafał Kukawski (http://blog.kukawski.pl)
  31. // + bugfixed by: omid (http://phpjs.org/functions/380:380#comment_137122)
  32. // + input by: Martin
  33. // + input by: Alex Wilson
  34. // + bugfixed by: Chris (http://www.devotis.nl/)
  35. // % note 1: Uses global: php_js to store the default timezone
  36. // % note 2: Although the function potentially allows timezone info (see notes), it currently does not set
  37. // % note 2: per a timezone specified by date_default_timezone_set(). Implementers might use
  38. // % note 2: this.php_js.currentTimezoneOffset and this.php_js.currentTimezoneDST set by that function
  39. // % note 2: in order to adjust the dates in this function (or our other date functions!) accordingly
  40. // * example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400);
  41. // * returns 1: '09:09:40 m is month'
  42. // * example 2: date('F j, Y, g:i a', 1062462400);
  43. // * returns 2: 'September 2, 2003, 2:26 am'
  44. // * example 3: date('Y W o', 1062462400);
  45. // * returns 3: '2003 36 2003'
  46. // * example 4: x = date('Y m d', (new Date()).getTime()/1000);
  47. // * example 4: (x+'').length == 10 // 2009 01 09
  48. // * returns 4: true
  49. // * example 5: date('W', 1104534000);
  50. // * returns 5: '53'
  51. // * example 6: date('B t', 1104534000);
  52. // * returns 6: '999 31'
  53. // * example 7: date('W U', 1293750000.82); // 2010-12-31
  54. // * returns 7: '52 1293750000'
  55. // * example 8: date('W', 1293836400); // 2011-01-01
  56. // * returns 8: '52'
  57. // * example 9: date('W Y-m-d', 1293974054); // 2011-01-02
  58. // * returns 9: '52 2011-01-02'
  59. var that = this,
  60. jsdate,
  61. f,
  62. formatChr = /\\?([a-z])/gi,
  63. formatChrCb,
  64. // Keep this here (works, but for code commented-out
  65. // below for file size reasons)
  66. //, tal= [],
  67. _pad = function (n, c) {
  68. n = n.toString();
  69. return n.length < c ? _pad('0' + n, c, '0') : n;
  70. },
  71. txt_words = ["Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  72. formatChrCb = function (t, s) {
  73. return f[t] ? f[t]() : s;
  74. };
  75. f = {
  76. // Day
  77. d: function () { // Day of month w/leading 0; 01..31
  78. return _pad(f.j(), 2);
  79. },
  80. D: function () { // Shorthand day name; Mon...Sun
  81. return f.l().slice(0, 3);
  82. },
  83. j: function () { // Day of month; 1..31
  84. return jsdate.getDate();
  85. },
  86. l: function () { // Full day name; Monday...Sunday
  87. return txt_words[f.w()] + 'day';
  88. },
  89. N: function () { // ISO-8601 day of week; 1[Mon]..7[Sun]
  90. return f.w() || 7;
  91. },
  92. S: function(){ // Ordinal suffix for day of month; st, nd, rd, th
  93. var j = f.j()
  94. i = j%10;
  95. if (i <= 3 && parseInt((j%100)/10) == 1) i = 0;
  96. return ['st', 'nd', 'rd'][i - 1] || 'th';
  97. },
  98. w: function () { // Day of week; 0[Sun]..6[Sat]
  99. return jsdate.getDay();
  100. },
  101. z: function () { // Day of year; 0..365
  102. var a = new Date(f.Y(), f.n() - 1, f.j()),
  103. b = new Date(f.Y(), 0, 1);
  104. return Math.round((a - b) / 864e5);
  105. },
  106. // Week
  107. W: function () { // ISO-8601 week number
  108. var a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3),
  109. b = new Date(a.getFullYear(), 0, 4);
  110. return _pad(1 + Math.round((a - b) / 864e5 / 7), 2);
  111. },
  112. // Month
  113. F: function () { // Full month name; January...December
  114. return txt_words[6 + f.n()];
  115. },
  116. m: function () { // Month w/leading 0; 01...12
  117. return _pad(f.n(), 2);
  118. },
  119. M: function () { // Shorthand month name; Jan...Dec
  120. return f.F().slice(0, 3);
  121. },
  122. n: function () { // Month; 1...12
  123. return jsdate.getMonth() + 1;
  124. },
  125. t: function () { // Days in month; 28...31
  126. return (new Date(f.Y(), f.n(), 0)).getDate();
  127. },
  128. // Year
  129. L: function () { // Is leap year?; 0 or 1
  130. var j = f.Y();
  131. return j % 4 === 0 & j % 100 !== 0 | j % 400 === 0;
  132. },
  133. o: function () { // ISO-8601 year
  134. var n = f.n(),
  135. W = f.W(),
  136. Y = f.Y();
  137. return Y + (n === 12 && W < 9 ? 1 : n === 1 && W > 9 ? -1 : 0);
  138. },
  139. Y: function () { // Full year; e.g. 1980...2010
  140. return jsdate.getFullYear();
  141. },
  142. y: function () { // Last two digits of year; 00...99
  143. return f.Y().toString().slice(-2);
  144. },
  145. // Time
  146. a: function () { // am or pm
  147. return jsdate.getHours() > 11 ? "pm" : "am";
  148. },
  149. A: function () { // AM or PM
  150. return f.a().toUpperCase();
  151. },
  152. B: function () { // Swatch Internet time; 000..999
  153. var H = jsdate.getUTCHours() * 36e2,
  154. // Hours
  155. i = jsdate.getUTCMinutes() * 60,
  156. // Minutes
  157. s = jsdate.getUTCSeconds(); // Seconds
  158. return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3);
  159. },
  160. g: function () { // 12-Hours; 1..12
  161. return f.G() % 12 || 12;
  162. },
  163. G: function () { // 24-Hours; 0..23
  164. return jsdate.getHours();
  165. },
  166. h: function () { // 12-Hours w/leading 0; 01..12
  167. return _pad(f.g(), 2);
  168. },
  169. H: function () { // 24-Hours w/leading 0; 00..23
  170. return _pad(f.G(), 2);
  171. },
  172. i: function () { // Minutes w/leading 0; 00..59
  173. return _pad(jsdate.getMinutes(), 2);
  174. },
  175. s: function () { // Seconds w/leading 0; 00..59
  176. return _pad(jsdate.getSeconds(), 2);
  177. },
  178. u: function () { // Microseconds; 000000-999000
  179. return _pad(jsdate.getMilliseconds() * 1000, 6);
  180. },
  181. // Timezone
  182. e: function () { // Timezone identifier; e.g. Atlantic/Azores, ...
  183. // The following works, but requires inclusion of the very large
  184. // timezone_abbreviations_list() function.
  185. /* return that.date_default_timezone_get();
  186. */
  187. throw 'Not supported (see source code of date() for timezone on how to add support)';
  188. },
  189. I: function () { // DST observed?; 0 or 1
  190. // Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC.
  191. // If they are not equal, then DST is observed.
  192. var a = new Date(f.Y(), 0),
  193. // Jan 1
  194. c = Date.UTC(f.Y(), 0),
  195. // Jan 1 UTC
  196. b = new Date(f.Y(), 6),
  197. // Jul 1
  198. d = Date.UTC(f.Y(), 6); // Jul 1 UTC
  199. return ((a - c) !== (b - d)) ? 1 : 0;
  200. },
  201. O: function () { // Difference to GMT in hour format; e.g. +0200
  202. var tzo = jsdate.getTimezoneOffset(),
  203. a = Math.abs(tzo);
  204. return (tzo > 0 ? "-" : "+") + _pad(Math.floor(a / 60) * 100 + a % 60, 4);
  205. },
  206. P: function () { // Difference to GMT w/colon; e.g. +02:00
  207. var O = f.O();
  208. return (O.substr(0, 3) + ":" + O.substr(3, 2));
  209. },
  210. T: function () { // Timezone abbreviation; e.g. EST, MDT, ...
  211. // The following works, but requires inclusion of the very
  212. // large timezone_abbreviations_list() function.
  213. /* var abbr = '', i = 0, os = 0, default = 0;
  214. if (!tal.length) {
  215. tal = that.timezone_abbreviations_list();
  216. }
  217. if (that.php_js && that.php_js.default_timezone) {
  218. default = that.php_js.default_timezone;
  219. for (abbr in tal) {
  220. for (i=0; i < tal[abbr].length; i++) {
  221. if (tal[abbr][i].timezone_id === default) {
  222. return abbr.toUpperCase();
  223. }
  224. }
  225. }
  226. }
  227. for (abbr in tal) {
  228. for (i = 0; i < tal[abbr].length; i++) {
  229. os = -jsdate.getTimezoneOffset() * 60;
  230. if (tal[abbr][i].offset === os) {
  231. return abbr.toUpperCase();
  232. }
  233. }
  234. }
  235. */
  236. return 'UTC';
  237. },
  238. Z: function () { // Timezone offset in seconds (-43200...50400)
  239. return -jsdate.getTimezoneOffset() * 60;
  240. },
  241. // Full Date/Time
  242. c: function () { // ISO-8601 date.
  243. return 'Y-m-d\\TH:i:sP'.replace(formatChr, formatChrCb);
  244. },
  245. r: function () { // RFC 2822
  246. return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb);
  247. },
  248. U: function () { // Seconds since UNIX epoch
  249. return jsdate / 1000 | 0;
  250. }
  251. };
  252. this.date = function (format, timestamp) {
  253. that = this;
  254. jsdate = (timestamp === undefined ? new Date() : // Not provided
  255. (timestamp instanceof Date) ? new Date(timestamp) : // JS Date()
  256. new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
  257. );
  258. return format.replace(formatChr, formatChrCb);
  259. };
  260. return this.date(format, timestamp);
  261. }