date.php 7.8 KB

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