date.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. /**
  3. * date.php
  4. *
  5. * Copyright (c) 1999-2005 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * Takes a date and parses it into a usable format. The form that a
  9. * date SHOULD arrive in is:
  10. * <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT)
  11. * (as specified in RFC 822) -- 'Tue' is optional
  12. *
  13. * @version $Id$
  14. * @package squirrelmail
  15. * @subpackage date
  16. */
  17. /** Load up some useful constants */
  18. require_once(SM_PATH . 'functions/constants.php');
  19. /**
  20. * Corrects a time stamp to be the local time.
  21. *
  22. * @param int stamp the timestamp to adjust
  23. * @param string tzc the timezone correction
  24. * @return int the corrected timestamp
  25. */
  26. function getGMTSeconds($stamp, $tzc) {
  27. /* date couldn't be parsed */
  28. if ($stamp == -1) {
  29. return -1;
  30. }
  31. /* timezone correction, expressed as `shhmm' */
  32. switch($tzc)
  33. {
  34. case 'Pacific':
  35. case 'PST':
  36. $tzc = '-0800';
  37. break;
  38. case 'Mountain':
  39. case 'MST':
  40. case 'PDT':
  41. $tzc = '-0700';
  42. break;
  43. case 'Central':
  44. case 'CST':
  45. case 'MDT':
  46. $tzc = '-0600';
  47. break;
  48. case 'Eastern':
  49. case 'EST':
  50. case 'CDT':
  51. $tzc = '-0500';
  52. break;
  53. case 'EDT':
  54. $tzc = '-0400';
  55. break;
  56. case 'GMT':
  57. $tzc = '+0000';
  58. break;
  59. case 'BST':
  60. case 'MET':
  61. case 'CET':
  62. $tzc = '+0100';
  63. break;
  64. case 'EET':
  65. case 'IST':
  66. case 'MET DST':
  67. case 'METDST':
  68. $tzc = '+0200';
  69. break;
  70. case 'HKT':
  71. $tzc = '+0800';
  72. break;
  73. case 'JST':
  74. case 'KST':
  75. $tzc = '+0900';
  76. break;
  77. }
  78. $neg = false;
  79. if (substr($tzc, 0, 1) == '-') {
  80. $neg = true;
  81. } else if (substr($tzc, 0, 1) != '+') {
  82. $tzc = '+'.$tzc;
  83. }
  84. $hh = substr($tzc,1,2);
  85. $mm = substr($tzc,3,2);
  86. $iTzc = ($hh * 60 + $mm) * 60;
  87. if ($neg) $iTzc = -1 * (int) $iTzc;
  88. /* stamp in gmt */
  89. $stamp -= $iTzc;
  90. /** now find what the server is at **/
  91. $current = date('Z', time());
  92. /* stamp in local timezone */
  93. $stamp += $current;
  94. return $stamp;
  95. }
  96. /**
  97. * Returns the (localized) string for a given day number.
  98. * Switch system has been intentionaly chosen for the
  99. * internationalization of month and day names. The reason
  100. * is to make sure that _("") strings will go into the
  101. * main po.
  102. *
  103. * @param int day_number the day number
  104. * @return string the day in human readable form
  105. */
  106. function getDayName( $day_number ) {
  107. switch( $day_number ) {
  108. case 0:
  109. $ret = _("Sunday");
  110. break;
  111. case 1:
  112. $ret = _("Monday");
  113. break;
  114. case 2:
  115. $ret = _("Tuesday");
  116. break;
  117. case 3:
  118. $ret = _("Wednesday");
  119. break;
  120. case 4:
  121. $ret = _("Thursday");
  122. break;
  123. case 5:
  124. $ret = _("Friday");
  125. break;
  126. case 6:
  127. $ret = _("Saturday");
  128. break;
  129. default:
  130. $ret = '';
  131. }
  132. return( $ret );
  133. }
  134. /**
  135. * Like getDayName, but returns the short form
  136. * @param int day_number the day number
  137. * @return string the day in short human readable form
  138. */
  139. function getDayAbrv( $day_number ) {
  140. switch( $day_number ) {
  141. case 0:
  142. $ret = _("Sun");
  143. break;
  144. case 1:
  145. $ret = _("Mon");
  146. break;
  147. case 2:
  148. $ret = _("Tue");
  149. break;
  150. case 3:
  151. $ret = _("Wed");
  152. break;
  153. case 4:
  154. $ret = _("Thu");
  155. break;
  156. case 5:
  157. $ret = _("Fri");
  158. break;
  159. case 6:
  160. $ret = _("Sat");
  161. break;
  162. default:
  163. $ret = '';
  164. }
  165. return( $ret );
  166. }
  167. /**
  168. * Returns the (localized) string for a given month number.
  169. *
  170. * @param string month_number the month number (01..12)
  171. * @return string the month name in human readable form
  172. */
  173. function getMonthName( $month_number ) {
  174. switch( $month_number ) {
  175. case '01':
  176. $ret = _("January");
  177. break;
  178. case '02':
  179. $ret = _("February");
  180. break;
  181. case '03':
  182. $ret = _("March");
  183. break;
  184. case '04':
  185. $ret = _("April");
  186. break;
  187. case '05':
  188. $ret = _("May");
  189. break;
  190. case '06':
  191. $ret = _("June");
  192. break;
  193. case '07':
  194. $ret = _("July");
  195. break;
  196. case '08':
  197. $ret = _("August");
  198. break;
  199. case '09':
  200. $ret = _("September");
  201. break;
  202. case '10':
  203. $ret = _("October");
  204. break;
  205. case '11':
  206. $ret = _("November");
  207. break;
  208. case '12':
  209. $ret = _("December");
  210. break;
  211. default:
  212. $ret = '';
  213. }
  214. return( $ret );
  215. }
  216. /**
  217. * Returns the (localized) string for a given month number,
  218. * short representation.
  219. *
  220. * @param string month_number the month number (01..12)
  221. * @return string the shortened month in human readable form
  222. */
  223. function getMonthAbrv( $month_number ) {
  224. switch( $month_number ) {
  225. case '01':
  226. $ret = _("Jan");
  227. break;
  228. case '02':
  229. $ret = _("Feb");
  230. break;
  231. case '03':
  232. $ret = _("Mar");
  233. break;
  234. case '04':
  235. $ret = _("Apr");
  236. break;
  237. case '05':
  238. $ret = _("Ma&#121;");
  239. break;
  240. case '06':
  241. $ret = _("Jun");
  242. break;
  243. case '07':
  244. $ret = _("Jul");
  245. break;
  246. case '08':
  247. $ret = _("Aug");
  248. break;
  249. case '09':
  250. $ret = _("Sep");
  251. break;
  252. case '10':
  253. $ret = _("Oct");
  254. break;
  255. case '11':
  256. $ret = _("Nov");
  257. break;
  258. case '12':
  259. $ret = _("Dec");
  260. break;
  261. default:
  262. $ret = '';
  263. }
  264. return( $ret );
  265. }
  266. /**
  267. * Returns the localized representation of the date/time.
  268. *
  269. * @param string date_format The format for the date, like the input for the PHP date() function.
  270. * @param int stamp the timestamp to convert
  271. * @return string a full date representation
  272. */
  273. function date_intl( $date_format, $stamp ) {
  274. $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format );
  275. // to reduce the date calls we retrieve m and w in the same call
  276. $ret = date('w#m#'. $ret, $stamp );
  277. // extract day and month in order to replace later by intl day and month
  278. $aParts = explode('#',$ret);
  279. $ret = str_replace(array('$1','$4','$2','$3',), array(getDayAbrv($aParts[0]),
  280. getMonthAbrv($aParts[1]),
  281. getMonthName($aParts[1]),
  282. getDayName($aParts[0])),
  283. $aParts[2]);
  284. return( $ret );
  285. }
  286. /**
  287. * This returns a date of the format "Wed, Oct 29, 2003 9:52 am",
  288. * or the same in 24H format (depending on the user's settings),
  289. * and taking localization into accout.
  290. *
  291. * @param int stamp the timestamp
  292. * @return string the long date string
  293. */
  294. function getLongDateString( $stamp ) {
  295. global $hour_format;
  296. if ($stamp == -1) {
  297. return '';
  298. }
  299. if ( $hour_format == SMPREF_TIME_12HR ) {
  300. $date_format = _("D, F j, Y g:i a");
  301. } else {
  302. $date_format = _("D, F j, Y G:i");
  303. }
  304. return( date_intl( $date_format, $stamp ) );
  305. }
  306. /**
  307. * Returns a short representation of the date,
  308. * taking timezones and localization into account.
  309. * Depending on user's settings, this string can be
  310. * of the form: "14:23" or "Jun 14, 2003" depending
  311. * on whether the stamp is "today" or not.
  312. *
  313. * @param int stamp the timestamp
  314. * @return string the date string
  315. */
  316. function getDateString( $stamp ) {
  317. global $invert_time, $hour_format, $show_full_date;
  318. if ( $stamp == -1 ) {
  319. return '';
  320. }
  321. $now = time();
  322. $dateZ = date('Z', $now );
  323. // FIXME: isn't this obsolete and introduced as a terrible workaround
  324. // for bugs at other places which are fixed a long time ago?
  325. if ($invert_time) {
  326. $dateZ = - $dateZ;
  327. }
  328. // calculate when it was midnight and when it will be,
  329. // in order to display dates differently if they're 'today'
  330. $midnight = $now - ($now % 86400) - $dateZ;
  331. // this is to correct if after calculations midnight is more than
  332. // one whole day away.
  333. if ($now - $midnight > 86400) {
  334. $midnight += 86400;
  335. }
  336. $nextmid = $midnight + 86400;
  337. if (($show_full_date == 1) || ($nextmid < $stamp)) {
  338. $date_format = _("M j, Y");
  339. } else if ($midnight < $stamp) {
  340. /* Today */
  341. if ( $hour_format == SMPREF_TIME_12HR ) {
  342. $date_format = _("g:i a");
  343. } else {
  344. $date_format = _("G:i");
  345. }
  346. } else if ($midnight - 518400 < $stamp) {
  347. /* This week */
  348. if ( $hour_format == SMPREF_TIME_12HR ) {
  349. $date_format = _("D, g:i a");
  350. } else {
  351. $date_format = _("D, G:i");
  352. }
  353. } else {
  354. /* before this week */
  355. $date_format = _("M j, Y");
  356. }
  357. return( date_intl( $date_format, $stamp ) );
  358. }
  359. /**
  360. * Decodes a RFC 822 Date-header into a timestamp
  361. *
  362. * @param array dateParts the Date-header split by whitespace
  363. * @return int the timestamp calculated from the header
  364. */
  365. function getTimeStamp($dateParts) {
  366. /** $dateParts[0] == <day of week> Mon, Tue, Wed
  367. ** $dateParts[1] == <day of month> 23
  368. ** $dateParts[2] == <month> Jan, Feb, Mar
  369. ** $dateParts[3] == <year> 1999
  370. ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
  371. ** $dateParts[5] == <from GMT> +0100
  372. ** $dateParts[6] == <zone> (EDT)
  373. **
  374. ** NOTE: In RFC 822, it states that <day of week> is optional.
  375. ** In that case, dateParts[0] would be the <day of month>
  376. ** and everything would be bumped up one.
  377. **/
  378. /*
  379. * Simply check to see if the first element in the dateParts
  380. * array is an integer or not.
  381. * Since the day of week is optional, this check is needed.
  382. */
  383. if (count($dateParts) <2) {
  384. return -1;
  385. } else if (count($dateParts) ==3) {
  386. if (substr_count($dateParts[0],'-') == 2 &&
  387. substr_count($dateParts[1],':') == 2) {
  388. // dd-Month-yyyy 23:19:05 +0200
  389. // redefine the date
  390. $aDate = explode('-',$dateParts[0]);
  391. $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
  392. $dateParts = $newDate;
  393. }
  394. }
  395. /* remove day of week */
  396. if (!is_numeric(trim($dateParts[0]))) {
  397. $dataParts = array_shift($dateParts);
  398. }
  399. /* calculate timestamp separated from the zone and obs-zone */
  400. $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
  401. if (!isset($dateParts[0])) {
  402. $dateParts[0] = '+0000';
  403. }
  404. if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
  405. /* zone in obs-zone format */
  406. if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
  407. $obs_zone = $regs[1];
  408. } else {
  409. $obs_zone = $dateParts[0];
  410. }
  411. return getGMTSeconds($stamp, $obs_zone);
  412. } else {
  413. return getGMTSeconds($stamp, $dateParts[0]);
  414. }
  415. }
  416. /* I use this function for profiling. Should never be called in
  417. actual versions of SquirrelMail released to public. */
  418. /*
  419. function getmicrotime() {
  420. $mtime = microtime();
  421. $mtime = explode(' ',$mtime);
  422. $mtime = $mtime[1] + $mtime[0];
  423. return ($mtime);
  424. }
  425. */
  426. ?>