FeedDate.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * FeedDate is an internal class that stores a date for a feed or feed item.
  4. * Usually, you won't need to use this.
  5. */
  6. class FeedDate
  7. {
  8. protected $unix;
  9. /**
  10. * Creates a new instance of FeedDate representing a given date.
  11. * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
  12. *
  13. * @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and
  14. * time is used.
  15. */
  16. public function __construct($dateString = "")
  17. {
  18. if ($dateString == "") {
  19. $dateString = date("r");
  20. }
  21. if (is_integer($dateString)) {
  22. $this->unix = $dateString;
  23. return;
  24. }
  25. $tzOffset = 0;
  26. if (preg_match(
  27. "~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",
  28. $dateString,
  29. $matches
  30. )) {
  31. $months = Array(
  32. "Jan" => 1,
  33. "Feb" => 2,
  34. "Mar" => 3,
  35. "Apr" => 4,
  36. "May" => 5,
  37. "Jun" => 6,
  38. "Jul" => 7,
  39. "Aug" => 8,
  40. "Sep" => 9,
  41. "Oct" => 10,
  42. "Nov" => 11,
  43. "Dec" => 12,
  44. );
  45. $this->unix = mktime($matches[4], $matches[5], $matches[6], $months[$matches[2]], $matches[1], $matches[3]);
  46. if (substr($matches[7], 0, 1) == '+' OR substr($matches[7], 0, 1) == '-') {
  47. $tzOffset = (((int)substr($matches[7], 0, 3) * 60) + (int)substr($matches[7], -2)) * 60;
  48. } else {
  49. if (strlen($matches[7]) == 1) {
  50. $oneHour = 3600;
  51. $ord = ord($matches[7]);
  52. if ($ord < ord("M")) {
  53. $tzOffset = (ord("A") - $ord - 1) * $oneHour;
  54. } elseif ($ord >= ord("M") AND $matches[7] != "Z") {
  55. $tzOffset = ($ord - ord("M")) * $oneHour;
  56. } elseif ($matches[7] == "Z") {
  57. $tzOffset = 0;
  58. }
  59. }
  60. switch ($matches[7]) {
  61. case "UT":
  62. case "GMT":
  63. $tzOffset = 0;
  64. }
  65. }
  66. $this->unix += $tzOffset;
  67. return;
  68. }
  69. if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~", $dateString, $matches)) {
  70. $this->unix = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
  71. if (substr($matches[7], 0, 1) == '+' OR substr($matches[7], 0, 1) == '-') {
  72. $tzOffset = (((int)substr($matches[7], 0, 3) * 60) + (int)substr($matches[7], -2)) * 60;
  73. } else {
  74. if ($matches[7] == "Z") {
  75. $tzOffset = 0;
  76. }
  77. }
  78. $this->unix += $tzOffset;
  79. return;
  80. }
  81. $this->unix = 0;
  82. }
  83. /**
  84. * Gets the date stored in this FeedDate as an RFC 822 date.
  85. *
  86. * @return string a date in RFC 822 format
  87. */
  88. public function rfc822()
  89. {
  90. //return gmdate("r",$this->unix);
  91. $date = gmdate("D, d M Y H:i:s O", $this->unix);
  92. return $date;
  93. }
  94. /**
  95. * Gets the date stored in this FeedDate as an ISO 8601 date.
  96. *
  97. * @return string a date in ISO 8601 format
  98. */
  99. public function iso8601()
  100. {
  101. $date = gmdate("Y-m-d\TH:i:sP", $this->unix);
  102. return $date;
  103. }
  104. /**
  105. * Gets the date stored in this FeedDate as unix time stamp.
  106. *
  107. * @return int a date as a unix time stamp
  108. */
  109. public function unix()
  110. {
  111. return $this->unix;
  112. }
  113. }