date.php 12 KB

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