PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/generator/skeleton/en/app/helpers/Date.php

https://github.com/reoring/sabel
PHP | 389 lines | 308 code | 72 blank | 9 comment | 16 complexity | 23baf025a490a9b6a56fce08ec2ee8dc MD5 | raw file
  1. <?php
  2. /**
  3. * Helpers_Date
  4. *
  5. * @category Helper
  6. * @package org.sabel.helper
  7. * @author Ebine Yutaka <ebine.yutaka@sabel.jp>
  8. * @copyright 2004-2008 Mori Reo <mori.reo@sabel.jp>
  9. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  10. */
  11. class Helpers_Date
  12. {
  13. const NORMAL = 0;
  14. const ATOM = 1;
  15. const RSS = 2;
  16. const COOKIE = 3;
  17. const ISO = 4;
  18. const RFC822 = 5;
  19. const RFC850 = 6;
  20. const RFC1036 = 7;
  21. const RFC1123 = 8;
  22. const RFC2822 = 9;
  23. const RFC = 10;
  24. const W3C = 11;
  25. private static $formats = array(self::NORMAL => array(
  26. "all" => "Y-m-d H:i:s",
  27. "date" => "Y-m-d",
  28. "time" => "H:i:s"),
  29. self::ATOM => array(
  30. "all" => "c",
  31. "date" => "Y-m-d",
  32. "time" => "H:i:sP"),
  33. self::RSS => array(
  34. "all" => "r",
  35. "date" => "D, d M Y",
  36. "time" => "H:i:s O"),
  37. self::COOKIE => array(
  38. "all" => "l, d-M-y H:i:s T",
  39. "date" => "l, d-M-y",
  40. "time" => "H:i:s T"),
  41. self::ISO => array(
  42. "all" => "Y-m-d\TH:i:sO",
  43. "date" => "Y-m-d",
  44. "time" => "H:i:sO"),
  45. self::RFC822 => array(
  46. "all" => "D, d M y H:i:s O",
  47. "date" => "D, d M y",
  48. "time" => "H:i:s O"),
  49. self::RFC850 => array(
  50. "all" => "l, d-M-y H:i:s T",
  51. "date" => "l, d-M-y",
  52. "time" => "H:i:s T"),
  53. self::RFC1036 => array(
  54. "all" => "D, d M y H:i:s O",
  55. "date" => "D, d M y",
  56. "time" => "H:i:s O"),
  57. self::RFC1123 => array(
  58. "all" => "r",
  59. "date" => "D, d M Y",
  60. "time" => "H:i:s O"),
  61. self::RFC2822 => array(
  62. "all" => "r",
  63. "date" => "D, d M Y",
  64. "time" => "H:i:s O"),
  65. self::RFC => array(
  66. "all" => "r",
  67. "date" => "D, d M Y",
  68. "time" => "H:i:s O"),
  69. self::W3C => array(
  70. "all" => "c",
  71. "date" => "Y-m-d",
  72. "time" => "H:i:sP"));
  73. protected
  74. $timestamp = null,
  75. $format = self::NORMAL;
  76. public static function format($date, $format)
  77. {
  78. return date(self::$formats[$format]["all"], strtotime($date));
  79. }
  80. public function __construct($arg = null)
  81. {
  82. if ($arg === null) {
  83. $this->timestamp = time();
  84. } elseif (is_string($arg)) {
  85. $this->timestamp = strtotime($arg);
  86. } elseif (is_array($arg)) {
  87. $y = (isset($arg["y"])) ? $arg["y"] : date("Y");
  88. $m = (isset($arg["m"])) ? $arg["m"] : date("m");
  89. $d = (isset($arg["d"])) ? $arg["d"] : 1;
  90. $h = (isset($arg["h"])) ? $arg["h"] : 0;
  91. $i = (isset($arg["i"])) ? $arg["i"] : 0;
  92. $s = (isset($arg["s"])) ? $arg["s"] : 0;
  93. $this->timestamp = mktime($h, $i, $s, $m, $d, $y);
  94. } else {
  95. throw new Exception("Helpers_Date::__construct() invalid parameter.");
  96. }
  97. }
  98. public function __toString()
  99. {
  100. return $this->getDateTime();
  101. }
  102. public function setFormat($format)
  103. {
  104. $this->format = $format;
  105. return $this;
  106. }
  107. public function getDatetime()
  108. {
  109. return date(self::$formats[$this->format]["all"], $this->timestamp);
  110. }
  111. public function getDate()
  112. {
  113. return date(self::$formats[$this->format]["date"], $this->timestamp);
  114. }
  115. public function getTime()
  116. {
  117. return date(self::$formats[$this->format]["time"], $this->timestamp);
  118. }
  119. public function getYear($twoDigits = false)
  120. {
  121. if ($twoDigits) {
  122. return date("y", $this->timestamp);
  123. } else {
  124. return date("Y", $this->timestamp);
  125. }
  126. }
  127. public function getMonth($withLeadingZeros = true)
  128. {
  129. if ($withLeadingZeros) {
  130. return date("m", $this->timestamp);
  131. } else {
  132. return date("n", $this->timestamp);
  133. }
  134. }
  135. public function getTextualMonth($short = true)
  136. {
  137. if ($short) {
  138. return date("M", $this->timestamp);
  139. } else {
  140. return date("F", $this->timestamp);
  141. }
  142. }
  143. public function getDay($withLeadingZeros = true)
  144. {
  145. if ($withLeadingZeros) {
  146. return date("d", $this->timestamp);
  147. } else {
  148. return date("j", $this->timestamp);
  149. }
  150. }
  151. public function getLastDay()
  152. {
  153. $timestamp = mktime($this->getHour(),
  154. $this->getMinute(),
  155. $this->getSecond(),
  156. $this->getMonth() + 1,
  157. 0,
  158. $this->getYear());
  159. return date("d", $timestamp);
  160. }
  161. public function getHour($withLeadingZeros = true)
  162. {
  163. if ($withLeadingZeros) {
  164. return date("H", $this->timestamp);
  165. } else {
  166. return date("G", $this->timestamp);
  167. }
  168. }
  169. public function getHourBy12Format($withLeadingZeros = true)
  170. {
  171. if ($withLeadingZeros) {
  172. return date("h", $this->timestamp);
  173. } else {
  174. return date("g", $this->timestamp);
  175. }
  176. }
  177. public function getMinute()
  178. {
  179. return date("i", $this->timestamp);
  180. }
  181. public function getSecond()
  182. {
  183. return date("s", $this->timestamp);
  184. }
  185. public function getMeridiem($lower = true)
  186. {
  187. return date(($lower) ? "a" : "A", $this->timestamp);
  188. }
  189. public function getTextualWeek($short = true)
  190. {
  191. if ($short) {
  192. return date("D", $this->timestamp);
  193. } else {
  194. return date("l", $this->timestamp);
  195. }
  196. }
  197. public function getWeekNumber()
  198. {
  199. return date("w", $this->timestamp);
  200. }
  201. public function ymd($sep = "-")
  202. {
  203. return $this->getYear() . $sep . $this->getMonth() . $sep . $this->getDay();
  204. }
  205. public function his($sep = ":")
  206. {
  207. return $this->getHour() . $sep . $this->getMinute() . $sep . $this->getSecond();
  208. }
  209. public function incYear($year = 1)
  210. {
  211. $year = $this->getYear() + $year;
  212. $this->timestamp = mktime($this->getHour(),
  213. $this->getMinute(),
  214. $this->getSecond(),
  215. $this->getMonth(),
  216. $this->getDay(),
  217. $year);
  218. return $year;
  219. }
  220. public function decYear($year = 1)
  221. {
  222. $year = $this->getYear() - $year;
  223. $this->timestamp = mktime($this->getHour(),
  224. $this->getMinute(),
  225. $this->getSecond(),
  226. $this->getMonth(),
  227. $this->getDay(),
  228. $year);
  229. return $year;
  230. }
  231. public function incMonth($month = 1)
  232. {
  233. $month = $this->getMonth() + $month;
  234. $this->timestamp = mktime($this->getHour(),
  235. $this->getMinute(),
  236. $this->getSecond(),
  237. $month,
  238. $this->getDay(),
  239. $this->getYear());
  240. return $month;
  241. }
  242. public function decMonth($month = 1)
  243. {
  244. $month = $this->getMonth() - $month;
  245. $this->timestamp = mktime($this->getHour(),
  246. $this->getMinute(),
  247. $this->getSecond(),
  248. $month,
  249. $this->getDay(),
  250. $this->getYear());
  251. return $month;
  252. }
  253. public function incDay($day = 1)
  254. {
  255. $this->timestamp += 86400 * $day;
  256. return $this->getDay();
  257. }
  258. public function decDay($day = 1)
  259. {
  260. $this->timestamp -= 86400 * $day;
  261. return $this->getDay();
  262. }
  263. public function incHour($hour = 1)
  264. {
  265. $this->timestamp += 3600 * $hour;
  266. return $this->getHour();
  267. }
  268. public function decHour($hour = 1)
  269. {
  270. $this->timestamp -= 3600 * $hour;
  271. return $this->getHour();
  272. }
  273. public function incMinute($min = 1)
  274. {
  275. $this->timestamp += 60 * $min;
  276. return $this->getMinute();
  277. }
  278. public function decMinute($min = 1)
  279. {
  280. $this->timestamp -= 60 * $min;
  281. return $this->getMinute();
  282. }
  283. public function incSecond($second = 1)
  284. {
  285. $this->timestamp += $second;
  286. return $this->getSecond();
  287. }
  288. public function decSecond($second = 1)
  289. {
  290. $this->timestamp -= $second;
  291. return $this->getSecond();
  292. }
  293. public function y()
  294. {
  295. return $this->getYear();
  296. }
  297. public function m()
  298. {
  299. return $this->getMonth();
  300. }
  301. public function d()
  302. {
  303. return $this->getDay();
  304. }
  305. public function h()
  306. {
  307. return $this->getHour();
  308. }
  309. public function i()
  310. {
  311. return $this->getMinute();
  312. }
  313. public function s()
  314. {
  315. return $this->getSecond();
  316. }
  317. }