date.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * date.php
  4. *
  5. * Copyright (c) 1999-2001 The SquirrelMail Development 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. * $Id$
  14. */
  15. /*****************************************************************/
  16. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  17. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  18. /*** + Base level indent should begin at left margin, as ***/
  19. /*** the first line of the function definition below. ***/
  20. /*** + All identation should consist of four space blocks ***/
  21. /*** + Tab characters are evil. ***/
  22. /*** + all comments should use "slash-star ... star-slash" ***/
  23. /*** style -- no pound characters, no slash-slash style ***/
  24. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  25. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  26. /*** + Please use ' instead of ", when possible. Note " ***/
  27. /*** should always be used in _( ) function calls. ***/
  28. /*** Thank you for your help making the SM code more readable. ***/
  29. /*****************************************************************/
  30. // corrects a time stamp to be the local time
  31. function getGMTSeconds($stamp, $gmt) {
  32. global $invert_time;
  33. if (($gmt == 'Pacific') || ($gmt == 'PST'))
  34. $gmt = '-0800';
  35. else if (($gmt == 'EDT'))
  36. $gmt = '-0400';
  37. else if (($gmt == 'Eastern') || ($gmt == 'EST') || ($gmt == 'CDT'))
  38. $gmt = '-0500';
  39. else if (($gmt == 'Central') || ($gmt == 'CST') || ($gmt == 'MDT'))
  40. $gmt = '-0600';
  41. else if (($gmt == 'Mountain') || ($gmt == 'MST') || ($gmt == 'PDT'))
  42. $gmt = '-0700';
  43. else if ($gmt == 'BST')
  44. $gmt = '+0100';
  45. else if ($gmt == 'EET')
  46. $gmt = '+0200';
  47. else if ($gmt == 'GMT')
  48. $gmt = '+0000';
  49. else if ($gmt == 'HKT')
  50. $gmt = '+0800';
  51. else if ($gmt == 'IST')
  52. $gmt = '+0200';
  53. else if ($gmt == 'JST')
  54. $gmt = '+0900';
  55. else if ($gmt == 'KST')
  56. $gmt = "+0900";
  57. else if ($gmt == 'MET')
  58. $gmt = '+0100';
  59. else if ($gmt == 'MET DST' || $gmt == 'METDST')
  60. $gmt = '+0200';
  61. if (substr($gmt, 0, 1) == '-') {
  62. $neg = true;
  63. $gmt = substr($gmt, 1, strlen($gmt));
  64. } else if (substr($gmt, 0, 1) == '+') {
  65. $neg = false;
  66. $gmt = substr($gmt, 1, strlen($gmt));
  67. } else
  68. $neg = false;
  69. $gmt = substr($gmt, 0, 2);
  70. $gmt = $gmt * 3600;
  71. if ($neg == true)
  72. $gmt = "-$gmt";
  73. else
  74. $gmt = "+$gmt";
  75. /** now find what the server is at **/
  76. $current = date('Z', time());
  77. if ($invert_time)
  78. $current = - $current;
  79. $stamp = (int)$stamp - (int)$gmt + (int)$current;
  80. return $stamp;
  81. }
  82. /**
  83. Switch system has been intentionaly choosed for the
  84. internationalization of month and day names. The reason
  85. is to make sure that _("") strings will go into the
  86. main po.
  87. **/
  88. function getDayName( $day_number ) {
  89. switch( $day_number ) {
  90. case 0:
  91. $ret = _("Sunday");
  92. break;
  93. case 1:
  94. $ret = _("Monday");
  95. break;
  96. case 2:
  97. $ret = _("Tuesday");
  98. break;
  99. case 3:
  100. $ret = _("Wednesday");
  101. break;
  102. case 4:
  103. $ret = _("Thursday");
  104. break;
  105. case 5:
  106. $ret = _("Friday");
  107. break;
  108. case 6:
  109. $ret = _("Saturday");
  110. break;
  111. default:
  112. $ret = '';
  113. }
  114. return( $ret );
  115. }
  116. function getMonthName( $month_number ) {
  117. switch( $month_number ) {
  118. case '01':
  119. $ret = _("January");
  120. break;
  121. case '02':
  122. $ret = _("February");
  123. break;
  124. case '03':
  125. $ret = _("March");
  126. break;
  127. case '04':
  128. $ret = _("April");
  129. break;
  130. case '05':
  131. $ret = _("May");
  132. break;
  133. case '06':
  134. $ret = _("June");
  135. break;
  136. case '07':
  137. $ret = _("July");
  138. break;
  139. case '08':
  140. $ret = _("August");
  141. break;
  142. case '09':
  143. $ret = _("September");
  144. break;
  145. case '10':
  146. $ret = _("October");
  147. break;
  148. case '11':
  149. $ret = _("November");
  150. break;
  151. case '12':
  152. $ret = _("December");
  153. break;
  154. default:
  155. $ret = '';
  156. }
  157. return( $ret );
  158. }
  159. function date_intl( $date_format, $stamp ) {
  160. $ret = str_replace( 'D', '$1', $date_format );
  161. $ret = str_replace( 'F', '$2', $ret );
  162. $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem
  163. $ret = str_replace( '$1', substr( getDayName( date( 'w', $stamp ) ), 0, 3 ), $ret );
  164. $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret );
  165. $ret = str_replace( '$3', '', $ret );
  166. return( $ret );
  167. }
  168. function getLongDateString( $stamp ) {
  169. return( date_intl( _("D, F j, Y g:i a"), $stamp ) );
  170. }
  171. function getDateString( $stamp ) {
  172. global $invert_time;
  173. $now = time();
  174. $dateZ = date('Z', $now );
  175. if ($invert_time)
  176. $dateZ = - $dateZ;
  177. $midnight = $now - ($now % 86400) - $dateZ;
  178. if ($midnight < $stamp) {
  179. // Today
  180. $date_format = _("g:i a");
  181. } else if ($midnight - 518400 < $stamp) {
  182. // This week
  183. $date_format = _("D, g:i a");
  184. } else {
  185. // before this week
  186. $date_format = _("M j, Y");
  187. }
  188. return( date_intl( $date_format, $stamp ) );
  189. // return( date( $date_i, $stamp ) );
  190. }
  191. function getTimeStamp($dateParts) {
  192. /** $dateParts[0] == <day of week> Mon, Tue, Wed
  193. ** $dateParts[1] == <day of month> 23
  194. ** $dateParts[2] == <month> Jan, Feb, Mar
  195. ** $dateParts[3] == <year> 1999
  196. ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
  197. ** $dateParts[5] == <from GMT> +0100
  198. ** $dateParts[6] == <zone> (EDT)
  199. **
  200. ** NOTE: In RFC 822, it states that <day of week> is optional.
  201. ** In that case, dateParts[0] would be the <day of month>
  202. ** and everything would be bumped up one.
  203. **/
  204. // Simply check to see if the first element in the dateParts
  205. // array is an integer or not.
  206. // Since the day of week is optional, this check is needed.
  207. //
  208. // The old code used eregi('mon|tue|wed|thu|fri|sat|sun',
  209. // $dateParts[0], $tmp) to find if the first element was the
  210. // day of week or day of month. This is an expensive call
  211. // (processing time) to have inside a loop. Doing it this way
  212. // saves quite a bit of time for large mailboxes.
  213. //
  214. // It is also quicker to call explode only once rather than
  215. // the 3 times it was getting called by calling the functions
  216. // getHour, getMinute, and getSecond.
  217. //
  218. if (! isset($dateParts[1])) $dateParts[1] = '';
  219. if (! isset($dateParts[2])) $dateParts[2] = '';
  220. if (! isset($dateParts[3])) $dateParts[3] = '';
  221. if (! isset($dateParts[4])) $dateParts[4] = '';
  222. if (! isset($dateParts[5])) $dateParts[5] = '';
  223. if (intval(trim($dateParts[0])) > 0) {
  224. $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
  225. $dateParts[2] . ' ' . $dateParts[3];
  226. return getGMTSeconds(strtotime($string), $dateParts[4]);
  227. }
  228. $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
  229. $dateParts[2] . ' ' . $dateParts[3] . ' ' . $dateParts[4];
  230. if (isset($dateParts[5]))
  231. return getGMTSeconds(strtotime($string), $dateParts[5]);
  232. else
  233. return getGMTSeconds(strtotime($string), '');
  234. }
  235. // I use this function for profiling. Should never be called in
  236. // actual versions of squirrelmail released to public.
  237. /*
  238. function getmicrotime() {
  239. $mtime = microtime();
  240. $mtime = explode(' ',$mtime);
  241. $mtime = $mtime[1] + $mtime[0];
  242. return ($mtime);
  243. }
  244. */
  245. ?>