date.php 7.2 KB

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