date.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * The date library of zentaopms.
  4. *
  5. * @copyright Copyright 2009-2015 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package Date
  9. * @version $Id: date.class.php 2605 2013-01-09 07:22:58Z wwccss $
  10. * @link http://www.zentao.net
  11. */
  12. class date
  13. {
  14. /**
  15. * Build hour time list.
  16. *
  17. * @param int $begin
  18. * @param int $end
  19. * @param int $delta
  20. * @access public
  21. * @return array
  22. */
  23. public static function buildTimeList($begin, $end, $delta)
  24. {
  25. $times = array();
  26. for($hour = $begin; $hour <= $end; $hour ++)
  27. {
  28. for($minutes = 0; $minutes < 60; $minutes += $delta)
  29. {
  30. $time = sprintf('%02d%02d', $hour, $minutes);
  31. $label = sprintf('%02d:%02d', $hour, $minutes);
  32. $times[$time] = $label;
  33. }
  34. }
  35. return $times;
  36. }
  37. /**
  38. * Get today.
  39. *
  40. * @access public
  41. * @return date
  42. */
  43. public static function today()
  44. {
  45. return date(DT_DATE1, time());
  46. }
  47. /**
  48. * Get yesterday
  49. *
  50. * @access public
  51. * @return date
  52. */
  53. public static function yesterday()
  54. {
  55. return date(DT_DATE1, strtotime('yesterday'));
  56. }
  57. /**
  58. * Get tomorrow.
  59. *
  60. * @access public
  61. * @return date
  62. */
  63. public static function tomorrow()
  64. {
  65. return date(DT_DATE1, strtotime('tomorrow'));
  66. }
  67. /**
  68. * Get the day before yesterday.
  69. *
  70. * @access public
  71. * @return date
  72. */
  73. public static function twoDaysAgo()
  74. {
  75. return date(DT_DATE1, strtotime('-2 days'));
  76. }
  77. /**
  78. * Get now time period.
  79. *
  80. * @param int $delta
  81. * @access public
  82. * @return string the current time period, like 0915
  83. */
  84. public static function now($delta = 10)
  85. {
  86. $range = range($delta, 60 - $delta, $delta);
  87. $hour = date('H', time());
  88. $minute = date('i', time());
  89. if($minute > 60 - $delta)
  90. {
  91. $hour += 1;
  92. $minute = 00;
  93. }
  94. else
  95. {
  96. for($i = 0; $i < $delta; $i ++)
  97. {
  98. if(in_array($minute + $i, $range))
  99. {
  100. $minute = $minute + $i;
  101. break;
  102. }
  103. }
  104. }
  105. return sprintf('%02d%02d', $hour, $minute);
  106. }
  107. /**
  108. * Format time 0915 to 09:15
  109. *
  110. * @param string $time
  111. * @access public
  112. * @return string
  113. */
  114. public static function formatTime($time)
  115. {
  116. if(strlen($time) != 4 or $time == '2400') return '';
  117. return substr($time, 0, 2) . ':' . substr($time, 2, 2);
  118. }
  119. /**
  120. * Get the begin and end date of next week.
  121. *
  122. * @access public
  123. * @return array
  124. */
  125. public static function getNextWeek()
  126. {
  127. $weekDay = date('N');
  128. $baseTime = time() + 86400 * 7;
  129. if($weekDay == 1) $baseTime = time() + 86400;
  130. if($weekDay == 7) $baseTime = time() - 86400;
  131. $begin = date(DT_DATE1, strtotime('last monday', $baseTime)) . ' 00:00:00';
  132. $end = date(DT_DATE1, strtotime('next sunday', $baseTime)) . ' 23:59:59';
  133. return array('begin' => $begin, 'end' => $end);
  134. }
  135. /**
  136. * Get the begin and end date of this week.
  137. *
  138. * @access public
  139. * @return array
  140. */
  141. public static function getThisWeek()
  142. {
  143. $baseTime = self::getMiddleOfThisWeek();
  144. $begin = date(DT_DATE1, strtotime('last monday', $baseTime)) . ' 00:00:00';
  145. $end = date(DT_DATE1, strtotime('next sunday', $baseTime)) . ' 23:59:59';
  146. return array('begin' => $begin, 'end' => $end);
  147. }
  148. /**
  149. * Get the begin and end date of last week.
  150. *
  151. * @access public
  152. * @return array
  153. */
  154. public static function getLastWeek()
  155. {
  156. $baseTime = self::getMiddleOfLastWeek();
  157. $begin = date(DT_DATE1, strtotime('last monday', $baseTime)) . ' 00:00:00';
  158. $end = date(DT_DATE1, strtotime('next sunday', $baseTime)) . ' 23:59:59';
  159. return array('begin' => $begin, 'end' => $end);
  160. }
  161. /**
  162. * Get the time at the middle of this week.
  163. *
  164. * If today in week is 1, move it one day in future. Else is 7, move it back one day. To keep the time geted in this week.
  165. *
  166. * @access public
  167. * @return time
  168. */
  169. public static function getMiddleOfThisWeek()
  170. {
  171. $baseTime = time();
  172. $weekDay = date('N');
  173. if($weekDay == 1) $baseTime = time() + 86400;
  174. if($weekDay == 7) $baseTime = time() - 86400;
  175. return $baseTime;
  176. }
  177. /**
  178. * Get middle of last week.
  179. *
  180. * @access public
  181. * @return time
  182. */
  183. public static function getMiddleOfLastWeek()
  184. {
  185. $baseTime = time();
  186. $weekDay = date('N');
  187. $baseTime = time() - 86400 * 7;
  188. if($weekDay == 1) $baseTime = time() - 86400 * 4; // Make sure is last thursday.
  189. if($weekDay == 7) $baseTime = time() - 86400 * 10; // Make sure is last thursday.
  190. return $baseTime;
  191. }
  192. /**
  193. * Get begin and end time of next month.
  194. *
  195. * @access public
  196. * @return array
  197. */
  198. public static function getNextMonth()
  199. {
  200. $thisYear = date('Y');
  201. $thisMonth = date('m');
  202. $year = $thisMonth == 12 ? $thisYear + 1 : $thisYear;
  203. $month = $thisMonth == 12 ? 1 : $thisMonth + 1;
  204. $begin = "{$year}-{$month}-01 00:00:00";
  205. $end = date("{$year}-{$month}-t 23:59:59", strtotime($begin));
  206. return array('begin' => $begin, 'end' => $end);
  207. }
  208. /**
  209. * Get begin and end time of this month.
  210. *
  211. * @access public
  212. * @return array
  213. */
  214. public static function getThisMonth()
  215. {
  216. $begin = date('Y-m') . '-01 00:00:00';
  217. $end = date('Y-m-d 23:59:59', strtotime("$begin +1 month -1 day"));
  218. return array('begin' => $begin, 'end' => $end);
  219. }
  220. /**
  221. * Get begin and end time of last month.
  222. *
  223. * @access public
  224. * @return array
  225. */
  226. public static function getLastMonth()
  227. {
  228. $begin = date('Y-m', strtotime('last month', strtotime(date('Y-m',time()) . '-01 00:00:01'))) . '-01 00:00:00';
  229. $end = date('Y-m-d 23:59:59', strtotime(date('Y-m-01') . ' -1 day'));
  230. return array('begin' => $begin, 'end' => $end);
  231. }
  232. /**
  233. * Get begin and end time of this season.
  234. *
  235. * @static
  236. * @access public
  237. * @return array
  238. */
  239. public static function getThisSeason()
  240. {
  241. $season = ceil((date('n')) / 3); // Get this session.
  242. $begin = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 2, 1, date('Y')));
  243. $endDay = date('t', mktime(0, 0 , 0, $season * 3, 1, date("Y"))); // Get end day.
  244. $end = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, $endDay, date('Y')));
  245. return array('begin' => $begin, 'end' => $end);
  246. }
  247. /**
  248. * Get begin and end time of last season.
  249. *
  250. * @static
  251. * @access public
  252. * @return array
  253. */
  254. public static function getLastSeason()
  255. {
  256. $season = ceil((date('n')) / 3) - 1; // Get last session.
  257. $begin = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 2, 1, date('Y')));
  258. $endDay = date('t', mktime(0, 0 , 0, $season * 3, 1, date("Y"))); // Get end day.
  259. $end = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, $endDay, date('Y')));
  260. return array('begin' => $begin, 'end' => $end);
  261. }
  262. /**
  263. * Get begin and end time of this year.
  264. *
  265. * @static
  266. * @access public
  267. * @return array
  268. */
  269. public static function getThisYear()
  270. {
  271. $begin = date(DT_DATE1, strtotime('1/1 this year')) . ' 00:00:00';
  272. $end = date(DT_DATE1, strtotime('1/1 next year -1 day')) . ' 23:59:59';
  273. return array('begin' => $begin, 'end' => $end);
  274. }
  275. /**
  276. * Get begin and end time of last year.
  277. *
  278. * @static
  279. * @access public
  280. * @return array
  281. */
  282. public static function getLastYear()
  283. {
  284. $begin = date(DT_DATE1, strtotime('1/1 last year')) . ' 00:00:00';
  285. $end = date(DT_DATE1, strtotime('1/1 this year -1 day')) . ' 23:59:59';
  286. return array('begin' => $begin, 'end' => $end);
  287. }
  288. /**
  289. * Get date list
  290. *
  291. * @param string $begin
  292. * @param string $end
  293. * @param string $format m/d/Y|Y-m-d
  294. * @param string $type noweekend|withweekend
  295. * @param int $weekend 2|1
  296. * @access public
  297. * @return array
  298. */
  299. public static function getDateList($begin, $end, $format = 'm/d/Y', $type = 'noweekend', $weekend = 2)
  300. {
  301. $begin = strtotime($begin);
  302. $end = strtotime($end);
  303. if($begin > $end) return array();
  304. $dateList = array();
  305. for($date = $begin; $date <= $end; $date += 24 * 3600)
  306. {
  307. $weekDay = date('w', $date);
  308. if(strpos($type, 'noweekend') !== false and (($weekend == 2 and $weekDay == 6) or $weekDay == 0)) continue;
  309. $dateList[] = date($format, $date);
  310. }
  311. return $dateList;
  312. }
  313. }