date.php 11 KB

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