date.php 11 KB

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