date.php 7.4 KB

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