date.php 4.5 KB

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