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

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

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