date.php 12 KB

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