date.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. ** $Id$
  11. **/
  12. if (defined ('date_php'))
  13. return;
  14. define ('date_php', true);
  15. // corrects a time stamp to be the local time
  16. function getGMTSeconds($stamp, $gmt) {
  17. global $invert_time;
  18. if (($gmt == 'Pacific') || ($gmt == 'PST'))
  19. $gmt = '-0800';
  20. else if (($gmt == 'EDT'))
  21. $gmt = '-0400';
  22. else if (($gmt == 'Eastern') || ($gmt == 'EST') || ($gmt == 'CDT'))
  23. $gmt = '-0500';
  24. else if (($gmt == 'Central') || ($gmt == 'CST') || ($gmt == 'MDT'))
  25. $gmt = '-0600';
  26. else if (($gmt == 'Mountain') || ($gmt == 'MST') || ($gmt == 'PDT'))
  27. $gmt = '-0700';
  28. else if ($gmt == 'BST')
  29. $gmt = '+0100';
  30. else if ($gmt == 'EET')
  31. $gmt = '+0200';
  32. else if ($gmt == 'GMT')
  33. $gmt = '+0000';
  34. else if ($gmt == 'HKT')
  35. $gmt = '+0800';
  36. else if ($gmt == 'IST')
  37. $gmt = '+0200';
  38. else if ($gmt == 'JST')
  39. $gmt = '+0900';
  40. else if ($gmt == 'KST')
  41. $gmt = "+0900";
  42. else if ($gmt == 'MET')
  43. $gmt = '+0100';
  44. else if ($gmt == 'MET DST' || $gmt == 'METDST')
  45. $gmt = '+0200';
  46. if (substr($gmt, 0, 1) == '-') {
  47. $neg = true;
  48. $gmt = substr($gmt, 1, strlen($gmt));
  49. } else if (substr($gmt, 0, 1) == '+') {
  50. $neg = false;
  51. $gmt = substr($gmt, 1, strlen($gmt));
  52. } else
  53. $neg = false;
  54. $gmt = substr($gmt, 0, 2);
  55. $gmt = $gmt * 3600;
  56. if ($neg == true)
  57. $gmt = "-$gmt";
  58. else
  59. $gmt = "+$gmt";
  60. /** now find what the server is at **/
  61. $current = date('Z', time());
  62. if ($invert_time)
  63. $current = - $current;
  64. $stamp = (int)$stamp - (int)$gmt + (int)$current;
  65. return $stamp;
  66. }
  67. function getLongDateString($stamp) {
  68. return date('D, F j, Y g:i a', $stamp);
  69. }
  70. function getDateString($stamp) {
  71. global $invert_time;
  72. $now = time();
  73. $dateZ = date('Z', $now);
  74. if ($invert_time)
  75. $dateZ = - $dateZ;
  76. $midnight = $now - ($now % 86400) - $dateZ;
  77. if ($midnight < $stamp) {
  78. // Today
  79. return date('g:i a', $stamp);
  80. } else if ($midnight - (60 * 60 * 24 * 6) < $stamp) {
  81. // This week
  82. return date('D, g:i a', $stamp);
  83. } else {
  84. // before this week
  85. return date('M j, Y', $stamp);
  86. }
  87. }
  88. function getTimeStamp($dateParts) {
  89. /** $dateParts[0] == <day of week> Mon, Tue, Wed
  90. ** $dateParts[1] == <day of month> 23
  91. ** $dateParts[2] == <month> Jan, Feb, Mar
  92. ** $dateParts[3] == <year> 1999
  93. ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
  94. ** $dateParts[5] == <from GMT> +0100
  95. ** $dateParts[6] == <zone> (EDT)
  96. **
  97. ** NOTE: In RFC 822, it states that <day of week> is optional.
  98. ** In that case, dateParts[0] would be the <day of month>
  99. ** and everything would be bumped up one.
  100. **/
  101. // Simply check to see if the first element in the dateParts
  102. // array is an integer or not.
  103. // Since the day of week is optional, this check is needed.
  104. //
  105. // The old code used eregi('mon|tue|wed|thu|fri|sat|sun',
  106. // $dateParts[0], $tmp) to find if the first element was the
  107. // day of week or day of month. This is an expensive call
  108. // (processing time) to have inside a loop. Doing it this way
  109. // saves quite a bit of time for large mailboxes.
  110. //
  111. // It is also quicker to call explode only once rather than
  112. // the 3 times it was getting called by calling the functions
  113. // getHour, getMinute, and getSecond.
  114. //
  115. if (intval(trim($dateParts[0])) > 0) {
  116. $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
  117. $dateParts[2] . ' ' . $dateParts[3];
  118. return getGMTSeconds(strtotime($string), $dateParts[4]);
  119. }
  120. $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' .
  121. $dateParts[2] . ' ' . $dateParts[3] . ' ' . $dateParts[4];
  122. if (isset($dateParts[5]))
  123. return getGMTSeconds(strtotime($string), $dateParts[5]);
  124. else
  125. return getGMTSeconds(strtotime($string), '');
  126. }
  127. // I use this function for profiling. Should never be called in
  128. // actual versions of squirrelmail released to public.
  129. function getmicrotime() {
  130. $mtime = microtime();
  131. $mtime = explode(' ',$mtime);
  132. $mtime = $mtime[1] + $mtime[0];
  133. return ($mtime);
  134. }
  135. ?>