DateController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. class DateController
  3. {
  4. private static $weekday = array("", "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So");
  5. private static $yesNo = array(false => "N", true => "J");
  6. private static $stringVars = array(
  7. "jahr" => "Year",
  8. "quartal" => "Quarter",
  9. "monat" => "Month",
  10. "tag" => "Day",
  11. "datum" => "Today",
  12. "zeit" => "TimeHm",
  13. "wochedesmonats" => "WeekOfMonth",
  14. "wochentag" => "Weekday",
  15. "tagdesjahres" => "DayOfYear",
  16. "ymd" => "FullDate",
  17. "vortag" => "FullDateYesterday"
  18. );
  19. private static $boolVars = array(
  20. "mo-fr" => "MoFr",
  21. "mo-sa" => "MoSa",
  22. "mo-so" => "MoSo",
  23. "di-sa" => "DiSa",
  24. "di-so" => "DiSo",
  25. "ersterwerktag" => "IsFirstWeekdayOfMonth",
  26. "ungeradewoche" => "OddWeek"
  27. );
  28. public static function Run ($argv)
  29. {
  30. $date = (count($argv) > 1) ? $argv[1] : "";
  31. $dateCtrl = new DateController($date);
  32. echo $dateCtrl->GetVariables();
  33. }
  34. private $timestamp;
  35. public function __construct ($date = "")
  36. {
  37. $this->timestamp = ($date == "") ? time() : strtotime($date);
  38. }
  39. public function GetVariables ()
  40. {
  41. $result = "Jahr=" . $this->Year() . "\r\n";
  42. $result .= "Quartal=" . $this->Quarter() . "\r\n";
  43. $result .= "Monat=" . $this->Month() . "\r\n";
  44. $result .= "Tag=" . $this->Day() . "\r\n";
  45. $result .= "Zeit=" . $this->TimeHm() . "\r\n";
  46. $result .= "WocheDesMonats=" . $this->WeekOfMonth() . "\r\n";
  47. $result .= "Wochentag=" . $this->Weekday() . "\r\n";
  48. $result .= "Mo-Fr=" . DateController::$yesNo[$this->MoFr()] . "\r\n";
  49. $result .= "Mo-Sa=" . DateController::$yesNo[$this->MoSa()] . "\r\n";
  50. $result .= "Mo-So=" . DateController::$yesNo[$this->MoSo()] . "\r\n";
  51. $result .= "Di-Sa=" . DateController::$yesNo[$this->DiSa()] . "\r\n";
  52. $result .= "Di-So=" . DateController::$yesNo[$this->DiSo()] . "\r\n";
  53. $result .= "ErsterWerktag=" . DateController::$yesNo[$this->IsFirstWeekdayOfMonth()] . "\r\n";
  54. $result .= "TagDesJahres=" . $this->DayOfYear() . "\r\n";
  55. $result .= "UngeradeWoche=" . DateController::$yesNo[$this->OddWeek()] . "\r\n";
  56. $result .= "datum=" . $this->Today() . "\r\n";
  57. $result .= "ymd=" . $this->FullDate() . "\r\n";
  58. $result .= "Vortag=" . $this->FullDateYesterday() . "\r\n";
  59. return $result;
  60. }
  61. public function GetValue ($key)
  62. {
  63. $key = trim(strtolower($key));
  64. if (isset(DateController::$stringVars[$key])) {
  65. return call_user_func(array($this, DateController::$stringVars[$key]));
  66. }
  67. if (isset(DateController::$boolVars[$key])) {
  68. return DateController::$yesNo[call_user_func(array($this, DateController::$boolVars[$key]))];
  69. }
  70. return "?";
  71. }
  72. public function Today ()
  73. {
  74. return date("d.m.Y", $this->timestamp);
  75. }
  76. public function FullDate ()
  77. {
  78. return date("Y-m-d", $this->timestamp);
  79. }
  80. public function FullDateYesterday ()
  81. {
  82. $yesterday = strtotime("-1 day", $this->timestamp);
  83. return date("Y-m-d", $yesterday);
  84. }
  85. public function Year ()
  86. {
  87. return date("Y", $this->timestamp);
  88. }
  89. public function Quarter ()
  90. {
  91. $month = $this->Month();
  92. if ($month <= "03") return "1";
  93. if ($month <= "06") return "2";
  94. if ($month <= "09") return "3";
  95. return "4";
  96. }
  97. public function Month ()
  98. {
  99. return date("m", $this->timestamp);
  100. }
  101. public function Day ()
  102. {
  103. return date("d", $this->timestamp);
  104. }
  105. public function TimeHm ()
  106. {
  107. return date("H:i", $this->timestamp);
  108. }
  109. public function Week ()
  110. {
  111. return date("W", $this->timestamp);
  112. }
  113. public function WeekOfMonth ()
  114. {
  115. $weekdayOfFirst = $this->WeekdayOfFirst();
  116. $weekOffset = ($weekdayOfFirst > 1 && $weekdayOfFirst < 6) ? 1 : 0;
  117. $offset = 7 - date("N", $this->timestamp);
  118. return floor((date("j", $this->timestamp) + $offset) / 7) + $weekOffset;
  119. }
  120. private function WeekdayOfFirst ()
  121. {
  122. return date("N", strtotime($this->Year()."-".$this->Month()."-01"));
  123. }
  124. public function Weekday ()
  125. {
  126. return DateController::$weekday[date("N", $this->timestamp)];
  127. }
  128. public function MoFr ()
  129. {
  130. return date("N", $this->timestamp) < 6;
  131. }
  132. public function MoSa ()
  133. {
  134. return date("N", $this->timestamp) != 7;
  135. }
  136. public function MoSo ()
  137. {
  138. return true;
  139. }
  140. public function DiSa ()
  141. {
  142. return date("N", $this->timestamp) != 1 && date("N", $this->timestamp) != 7;
  143. }
  144. public function DiSo ()
  145. {
  146. return date("N", $this->timestamp) != 1;
  147. }
  148. public function FirstWeekdayOfMonth ()
  149. {
  150. $weekdayOfFirst = $this->WeekdayOfFirst();
  151. if ($weekdayOfFirst == 6) return "03";
  152. if ($weekdayOfFirst == 7) return "02";
  153. return "01";
  154. }
  155. public function IsFirstWeekdayOfMonth ()
  156. {
  157. return $this->FirstWeekdayOfMonth() == $this->Day();
  158. }
  159. public function DayOfYear ()
  160. {
  161. return date("z", $this->timestamp) + 1;
  162. }
  163. public function OddWeek ()
  164. {
  165. return ($this->Week() % 2) == 1;
  166. }
  167. }