date.php 7.8 KB

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