date.php 4.5 KB

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