Source for file date.php

Documentation is available at date.php

  1. <?php
  2. /**
  3. * date.php
  4. *
  5. * Takes a date and parses it into a usable format. The form that a
  6. * date SHOULD arrive in is:
  7. * <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT)
  8. * (as specified in RFC 822) -- 'Tue' is optional
  9. *
  10. * @copyright 1999-2020 The SquirrelMail Project Team
  11. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  12. * @version $Id: date.php 14840 2020年01月07日 07:42:38Z pdontthink $
  13. * @package squirrelmail
  14. * @subpackage date
  15. */
  16. /** Load up some useful constants */
  17. require_once(SM_PATH . 'functions/constants.php');
  18. /**
  19. * Corrects a time stamp to be the local time.
  20. *
  21. * @param int stamp the timestamp to adjust
  22. * @param string tzc the timezone correction
  23. * @return int the corrected timestamp
  24. */
  25. function getGMTSeconds ($stamp, $tzc) {
  26. /* date couldn't be parsed */
  27. if ($stamp == -1) {
  28. return -1;
  29. }
  30. /* timezone correction, expressed as `shhmm' */
  31. switch($tzc)
  32. {
  33. case 'Pacific':
  34. case 'PST':
  35. $tzc = '-0800';
  36. break;
  37. case 'Mountain':
  38. case 'MST':
  39. case 'PDT':
  40. $tzc = '-0700';
  41. break;
  42. case 'Central':
  43. case 'CST':
  44. case 'MDT':
  45. $tzc = '-0600';
  46. break;
  47. case 'Eastern':
  48. case 'EST':
  49. case 'CDT':
  50. $tzc = '-0500';
  51. break;
  52. case 'EDT':
  53. $tzc = '-0400';
  54. break;
  55. case 'GMT':
  56. $tzc = '+0000';
  57. break;
  58. case 'BST':
  59. case 'MET':
  60. case 'CET':
  61. $tzc = '+0100';
  62. break;
  63. case 'EET':
  64. case 'IST':
  65. case 'MET DST':
  66. case 'METDST':
  67. case 'CEST':
  68. case 'MEST':
  69. $tzc = '+0200';
  70. break;
  71. case 'HKT':
  72. $tzc = '+0800';
  73. break;
  74. case 'JST':
  75. case 'KST':
  76. $tzc = '+0900';
  77. break;
  78. }
  79. $neg = false;
  80. if (substr ($tzc, 0, 1) == '-') {
  81. $neg = true;
  82. } else if (substr ($tzc, 0, 1) != '+') {
  83. $tzc = '+'.$tzc;
  84. }
  85. $hh = substr ($tzc,1,2);
  86. $mm = substr ($tzc,3,2);
  87. $iTzc = ($hh * 60 + $mm) * 60;
  88. if ($neg) $iTzc = -1 * (int) $iTzc;
  89. /* stamp in gmt */
  90. $stamp -= $iTzc;
  91. /** now find what the server is at **/
  92. $current = date ('Z', time ());
  93. /* stamp in local timezone */
  94. $stamp += $current;
  95. return $stamp;
  96. }
  97. /**
  98. * Returns the (localized) string for a given day number.
  99. * Switch system has been intentionaly chosen for the
  100. * internationalization of month and day names. The reason
  101. * is to make sure that _("") strings will go into the
  102. * main po.
  103. *
  104. * @param int day_number the day number
  105. * @return string the day in human readable form
  106. */
  107. function getDayName ( $day_number ) {
  108. switch( $day_number ) {
  109. case 0:
  110. $ret = _ ("Sunday");
  111. break;
  112. case 1:
  113. $ret = _ ("Monday");
  114. break;
  115. case 2:
  116. $ret = _ ("Tuesday");
  117. break;
  118. case 3:
  119. $ret = _ ("Wednesday");
  120. break;
  121. case 4:
  122. $ret = _ ("Thursday");
  123. break;
  124. case 5:
  125. $ret = _ ("Friday");
  126. break;
  127. case 6:
  128. $ret = _ ("Saturday");
  129. break;
  130. default:
  131. $ret = '';
  132. }
  133. return( $ret );
  134. }
  135. /**
  136. * Like getDayName, but returns the short form
  137. * @param int day_number the day number
  138. * @return string the day in short human readable form
  139. */
  140. function getDayAbrv ( $day_number ) {
  141. switch( $day_number ) {
  142. case 0:
  143. $ret = _ ("Sun");
  144. break;
  145. case 1:
  146. $ret = _ ("Mon");
  147. break;
  148. case 2:
  149. $ret = _ ("Tue");
  150. break;
  151. case 3:
  152. $ret = _ ("Wed");
  153. break;
  154. case 4:
  155. $ret = _ ("Thu");
  156. break;
  157. case 5:
  158. $ret = _ ("Fri");
  159. break;
  160. case 6:
  161. $ret = _ ("Sat");
  162. break;
  163. default:
  164. $ret = '';
  165. }
  166. return( $ret );
  167. }
  168. /**
  169. * Returns the (localized) string for a given month number.
  170. *
  171. * @param string month_number the month number (01..12)
  172. * @return string the month name in human readable form
  173. */
  174. function getMonthName ( $month_number ) {
  175. switch( $month_number ) {
  176. case '01':
  177. $ret = _ ("January");
  178. break;
  179. case '02':
  180. $ret = _ ("February");
  181. break;
  182. case '03':
  183. $ret = _ ("March");
  184. break;
  185. case '04':
  186. $ret = _ ("April");
  187. break;
  188. case '05':
  189. $ret = _ ("May");
  190. break;
  191. case '06':
  192. $ret = _ ("June");
  193. break;
  194. case '07':
  195. $ret = _ ("July");
  196. break;
  197. case '08':
  198. $ret = _ ("August");
  199. break;
  200. case '09':
  201. $ret = _ ("September");
  202. break;
  203. case '10':
  204. $ret = _ ("October");
  205. break;
  206. case '11':
  207. $ret = _ ("November");
  208. break;
  209. case '12':
  210. $ret = _ ("December");
  211. break;
  212. default:
  213. $ret = '';
  214. }
  215. return( $ret );
  216. }
  217. /**
  218. * Returns the (localized) string for a given month number,
  219. * short representation.
  220. *
  221. * @param string month_number the month number (01..12)
  222. * @return string the shortened month in human readable form
  223. */
  224. function getMonthAbrv ( $month_number ) {
  225. switch( $month_number ) {
  226. case '01':
  227. $ret = _ ("Jan");
  228. break;
  229. case '02':
  230. $ret = _ ("Feb");
  231. break;
  232. case '03':
  233. $ret = _ ("Mar");
  234. break;
  235. case '04':
  236. $ret = _ ("Apr");
  237. break;
  238. case '05':
  239. $ret = _ ("Ma&#121;");
  240. break;
  241. case '06':
  242. $ret = _ ("Jun");
  243. break;
  244. case '07':
  245. $ret = _ ("Jul");
  246. break;
  247. case '08':
  248. $ret = _ ("Aug");
  249. break;
  250. case '09':
  251. $ret = _ ("Sep");
  252. break;
  253. case '10':
  254. $ret = _ ("Oct");
  255. break;
  256. case '11':
  257. $ret = _ ("Nov");
  258. break;
  259. case '12':
  260. $ret = _ ("Dec");
  261. break;
  262. default:
  263. $ret = '';
  264. }
  265. return( $ret );
  266. }
  267. /**
  268. * Returns the localized representation of the date/time.
  269. *
  270. * @param string date_format The format for the date, like the input for the PHP date() function.
  271. * @param int stamp the timestamp to convert
  272. * @return string a full date representation
  273. */
  274. function date_intl ( $date_format, $stamp ) {
  275. $ret = str_replace ( array('D','F','l','M'), array('1ドル','2ドル','3ドル','4ドル'), $date_format );
  276. // to reduce the date calls we retrieve m and w in the same call
  277. $ret = date ('w#m#'. $ret, $stamp );
  278. // extract day and month in order to replace later by intl day and month
  279. $aParts = explode ('#',$ret);
  280. $ret = str_replace (array('1ドル','4ドル','2ドル','3ドル',), array(getDayAbrv ($aParts[0]),
  281. getMonthAbrv ($aParts[1]),
  282. getMonthName ($aParts[1]),
  283. getDayName ($aParts[0])),
  284. $aParts[2]);
  285. return( $ret );
  286. }
  287. /**
  288. * This returns a date of the format "Wed, Oct 29, 2003 9:52 am",
  289. * or the same in 24H format (depending on the user's settings),
  290. * and taking localization into accout.
  291. *
  292. * @param int stamp the timestamp
  293. * @param string fallback string to use when stamp not valid
  294. * @return string the long date string
  295. */
  296. function getLongDateString ( $stamp, $fallback = '' ) {
  297. global $hour_format;
  298. if ($stamp == -1) {
  299. return $fallback;
  300. }
  301. if ( $hour_format == SMPREF_TIME_12HR ) {
  302. $date_format = _ ("D, F j, Y g:i a");
  303. } else {
  304. $date_format = _ ("D, F j, Y H:i");
  305. }
  306. return( date_intl ( $date_format, $stamp ) );
  307. }
  308. /**
  309. * Returns a short representation of the date,
  310. * taking timezones and localization into account.
  311. * Depending on user's settings, this string can be
  312. * of the form: "14:23" or "Jun 14, 2003" depending
  313. * on whether the stamp is "today" or not.
  314. *
  315. * @param int $stamp The timestamp
  316. * @param boolean $return_full_date_and_time When TRUE,
  317. * ignore all
  318. * user settings
  319. * and use full
  320. * date and time
  321. * (OPTIONAL;
  322. * default FALSE)
  323. * @return string the date string
  324. */
  325. function getDateString ( $stamp, $return_full_date_and_time=FALSE ) {
  326. global $invert_time , $hour_format, $show_full_date;
  327. if ( $stamp == -1 ) {
  328. return '';
  329. }
  330. $now = time ();
  331. $dateZ = date ('Z', $now );
  332. // FIXME: isn't this obsolete and introduced as a terrible workaround
  333. // for bugs at other places which are fixed a long time ago?
  334. if ($invert_time) {
  335. $dateZ = - $dateZ;
  336. }
  337. // calculate when it was midnight and when it will be,
  338. // in order to display dates differently if they're 'today'
  339. $midnight = $now - ($now % 86400) - $dateZ;
  340. // this is to correct if after calculations midnight is more than
  341. // one whole day away.
  342. if ($now - $midnight > 86400) {
  343. $midnight += 86400;
  344. }
  345. $nextmid = $midnight + 86400;
  346. if ($return_full_date_and_time) {
  347. if ( $hour_format == SMPREF_TIME_12HR ) {
  348. $date_format = _ ("D, F j, Y g:i a");
  349. } else {
  350. $date_format = _ ("D, F j, Y H:i");
  351. }
  352. } else if (($show_full_date == 1) || ($nextmid < $stamp)) {
  353. $date_format = _ ("M j, Y");
  354. } else if ($midnight < $stamp) {
  355. /* Today */
  356. if ( $hour_format == SMPREF_TIME_12HR ) {
  357. $date_format = _ ("g:i a");
  358. } else {
  359. $date_format = _ ("H:i");
  360. }
  361. } else if ($midnight - 518400 < $stamp) {
  362. /* This week */
  363. if ( $hour_format == SMPREF_TIME_12HR ) {
  364. $date_format = _ ("D, g:i a");
  365. } else {
  366. $date_format = _ ("D, H:i");
  367. }
  368. } else {
  369. /* before this week */
  370. $date_format = _ ("M j, Y");
  371. }
  372. return( date_intl ( $date_format, $stamp ) );
  373. }
  374. /**
  375. * Decodes a RFC 822 Date-header into a timestamp
  376. *
  377. * @param array dateParts the Date-header split by whitespace
  378. * @return int the timestamp calculated from the header
  379. */
  380. function getTimeStamp ($dateParts) {
  381. /** $dateParts[0] == <day of week> Mon, Tue, Wed
  382. ** $dateParts[1] == <day of month> 23
  383. ** $dateParts[2] == <month> Jan, Feb, Mar
  384. ** $dateParts[3] == <year> 1999
  385. ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
  386. ** $dateParts[5] == <from GMT> +0100
  387. ** $dateParts[6] == <zone> (EDT)
  388. **
  389. ** NOTE: In RFC 822, it states that <day of week> is optional.
  390. ** In that case, dateParts[0] would be the <day of month>
  391. ** and everything would be bumped up one.
  392. **/
  393. if (count ($dateParts) <2) {
  394. return -1;
  395. } else if (count ($dateParts) ==3) {
  396. if (substr_count ($dateParts[0],'-') == 2 &&
  397. substr_count ($dateParts[1],':') == 2) {
  398. // dd-Month-yyyy 23:19:05 +0200
  399. // redefine the date
  400. $aDate = explode ('-',$dateParts[0]);
  401. $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
  402. $dateParts = $newDate;
  403. }
  404. }
  405. /*
  406. * Simply check to see if the first element in the dateParts
  407. * array is an integer or not.
  408. * Since the day of week is optional, this check is needed.
  409. */
  410. if (!is_numeric (trim ($dateParts[0]))) {
  411. /* cope with broken mailers that send "Tue,23" without space */
  412. if ( preg_match ('/^\w+,(\d{1,2})$/', $dateParts[0], $match) ) {
  413. /* replace Tue,23 with 23 */
  414. $dateParts[0] = $match[1];
  415. } else {
  416. /* just drop the day of the week */
  417. array_shift ($dateParts);
  418. }
  419. }
  420. /* calculate timestamp separated from the zone and obs-zone */
  421. $stamp = strtotime (implode (' ', array_splice ($dateParts,0,4)));
  422. if (!isset($dateParts[0])) {
  423. $dateParts[0] = '+0000';
  424. }
  425. if (!preg_match ('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
  426. /* zone in obs-zone format */
  427. if (preg_match ('/\((.+)\)/',$dateParts[0],$regs)) {
  428. $obs_zone = $regs[1];
  429. } else {
  430. $obs_zone = $dateParts[0];
  431. }
  432. return getGMTSeconds ($stamp, $obs_zone);
  433. } else {
  434. return getGMTSeconds ($stamp, $dateParts[0]);
  435. }
  436. }
  437. /* I use this function for profiling. Should never be called in
  438. actual versions of squirrelmail released to public. */
  439. /*
  440. function getmicrotime() {
  441. $mtime = microtime();
  442. $mtime = explode(' ',$mtime);
  443. $mtime = $mtime[1] + $mtime[0];
  444. return ($mtime);
  445. }
  446. */

Documentation generated on 2020年1月13日 04:24:26 +0100 by phpDocumentor 1.4.3

AltStyle によって変換されたページ (->オリジナル) /