PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/wind/utility/WindDate.php

https://gitlab.com/wuhang2003/phpwind
PHP | 332 lines | 188 code | 18 blank | 126 comment | 37 complexity | 8a21902af6b52c7a85aeb26037c3d17a MD5 | raw file
  1. <?php
  2. /**
  3. * 日期的换算与计算
  4. *
  5. * @author Qian Su <aoxue.1988.su.qian@163.com>
  6. * @copyright ©2003-2103 phpwind.com
  7. * @license http://www.windframework.com
  8. * @version $Id: WindDate.php 2973 2011-10-15 19:22:48Z yishuo $
  9. * @package utility
  10. */
  11. class WindDate {
  12. /**
  13. * 获取时区
  14. *
  15. * @return string
  16. */
  17. public static function getTimeZone() {
  18. return function_exists('date_default_timezone_get') ? date_default_timezone_get() : date('e');
  19. }
  20. /**
  21. * 设置时区
  22. *
  23. * @param string $timezone 时区
  24. */
  25. public static function setTimezone($timezone) {
  26. function_exists('date_default_timezone_set') ? date_default_timezone_set($timezone) : putenv("TZ={$timezone}");
  27. }
  28. /**
  29. * 格式化输出
  30. *
  31. * @param string $format 目标格式,默认为null则以Y-m-d H:i:s格式输出
  32. * @param int $dateTime unix时间戳,默认为null则用当前时间
  33. * @return string
  34. */
  35. public static function format($format = null, $dateTime = null) {
  36. return date($format ? $format : 'Y-m-d H:i:s', self::getTimeStamp($dateTime));
  37. }
  38. /**
  39. * 获取日期的某部分
  40. *
  41. * @param string $interval 字符串表达式 ,时间间隔类型
  42. * @param mixed $dateTime 表示日期的文字,默认为null则用当前时间
  43. * @return string 返回日期的某部分
  44. */
  45. public static function datePart($interval, $dateTime = null) {
  46. return date($interval, self::getTimeStamp($dateTime));
  47. }
  48. /**
  49. * 获取两个日期的差
  50. *
  51. * @param string $interval 返回两个日期差的间隔类型
  52. * @param mixed $startDateTime 开始日期
  53. * @param mixed $endDateTime 结束日期
  54. * @return string
  55. */
  56. public static function dateDiff($interval, $startDateTime, $endDateTime) {
  57. $diff = self::getTimeStamp($endDateTime) - self::getTimeStamp($startDateTime);
  58. $retval = 0;
  59. switch ($interval) {
  60. case "y":
  61. $retval = bcdiv($diff, (60 * 60 * 24 * 365));
  62. break;
  63. case "m":
  64. $retval = bcdiv($diff, (60 * 60 * 24 * 30));
  65. break;
  66. case "w":
  67. $retval = bcdiv($diff, (60 * 60 * 24 * 7));
  68. break;
  69. case "d":
  70. $retval = bcdiv($diff, (60 * 60 * 24));
  71. break;
  72. case "h":
  73. $retval = bcdiv($diff, (60 * 60));
  74. break;
  75. case "n":
  76. $retval = bcdiv($diff, 60);
  77. break;
  78. case "s":
  79. default:
  80. $retval = $diff;
  81. break;
  82. }
  83. return $retval;
  84. }
  85. /**
  86. * 返回向指定日期追加指定间隔类型的一段时间间隔后的日期
  87. *
  88. * @param string $interval 字符串表达式,是所要加上去的时间间隔类型。
  89. * @param int $value 数值表达式,是要加上的时间间隔的数目。其数值可以为正数(得到未来的日期),也可以为负数(得到过去的日期)。
  90. * @param string $dateTime 表示日期的文字,这一日期还加上了时间间隔。
  91. * @param mixed $format 格式化输出
  92. * @return string 返回追加后的时间
  93. */
  94. public static function dateAdd($interval, $value, $dateTime, $format = null) {
  95. $date = getdate(self::getTimeStamp($dateTime));
  96. switch ($interval) {
  97. case "y":
  98. $date["year"] += $value;
  99. break;
  100. case "q":
  101. $date["mon"] += ($value * 3);
  102. break;
  103. case "m":
  104. $date["mon"] += $value;
  105. break;
  106. case "w":
  107. $date["mday"] += ($value * 7);
  108. break;
  109. case "d":
  110. $date["mday"] += $value;
  111. break;
  112. case "h":
  113. $date["hours"] += $value;
  114. break;
  115. case "n":
  116. $date["minutes"] += $value;
  117. break;
  118. case "s":
  119. default:
  120. $date["seconds"] += $value;
  121. break;
  122. }
  123. return self::format($format, mktime($date["hours"], $date["minutes"], $date["seconds"], $date["mon"], $date["mday"], $date["year"]));
  124. }
  125. /**
  126. * 得到一年中每个月真实的天数
  127. *
  128. * @param string $year 需要获得的月份天数的年份
  129. * @return array 每月的天数组成的数组
  130. */
  131. public static function getRealDaysInMonthsOfYear($year) {
  132. $months = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  133. if (self::isLeapYear($year)) {
  134. $months[1] = 29;
  135. }
  136. return $months;
  137. }
  138. /**
  139. * 获取该月的天数
  140. *
  141. * @param int $month 月份
  142. * @param int $year 年份
  143. * @return int
  144. */
  145. public static function getDaysInMonth($month, $year) {
  146. if (1 > $month || 12 < $month) {
  147. return 0;
  148. }
  149. if (!($daysInmonths = self::getRealDaysInMonthsOfYear($year))) {
  150. return 0;
  151. }
  152. return $daysInmonths[$month - 1];
  153. }
  154. /**
  155. * 获取该年的天数
  156. *
  157. * @return int
  158. */
  159. public static function getDaysInYear($year) {
  160. return self::isLeapYear($year) ? 366 : 365;
  161. }
  162. /**
  163. * 取得RFC格式的日期与时间
  164. *
  165. * @param string $data 需要获取的时间,默认为null则获取当前时间
  166. * @return string
  167. */
  168. public static function getRFCDate($date = null) {
  169. $time = $date ? is_int($date) ? $date : strtotime($date) : time();
  170. $tz = date('Z', $time);
  171. $tzs = ($tz < 0) ? '-' : '+';
  172. $tz = abs($tz);
  173. $tz = (int) ($tz / 3600) * 100 + ($tz % 3600) / 60;
  174. return sprintf("%s %s%04d", date('D, j M Y H:i:s', $time), $tzs, $tz);
  175. }
  176. /**
  177. * 取得中国日期时间
  178. *
  179. * @param int $time 需要使用的时间戳,默认为null则获取当前时间戳
  180. * @return string
  181. */
  182. public static function getChinaDate($time = null) {
  183. list($y, $m, $d, $w, $h, $_h, $i) = explode(' ', date('Y n j w G g i', $time ? $time : time()));
  184. return sprintf('%s年%s月%s日(%s) %s%s:%s', $y, $m, $d, self::getChinaWeek($w), self::getPeriodOfTime($h), $_h, $i);
  185. }
  186. /**
  187. * 取得中国的星期
  188. *
  189. * @param int $week 处国人的星期,是一个数值,默认为null则使用当前时间
  190. * @return string
  191. */
  192. public static function getChinaWeek($week = null) {
  193. $week = $week ? $week : (int) date('w', time());
  194. $weekMap = array("星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  195. return $weekMap[$week];
  196. }
  197. /**
  198. * 取得一天中的时段
  199. *
  200. * @param int $hour 小时,默认为null则获取当前时间
  201. * @return string
  202. */
  203. public static function getPeriodOfTime($hour = null) {
  204. $hour = $hour ? $hour : (int) date('G', time());
  205. $period = '';
  206. if (0 <= $hour && 6 > $hour) {
  207. $period = '凌晨';
  208. } elseif (6 <= $hour && 8 > $hour) {
  209. $period = '早上';
  210. } elseif (8 <= $hour && 11 > $hour) {
  211. $period = '上午';
  212. } elseif (11 <= $hour && 13 > $hour) {
  213. $period = '中午';
  214. } elseif (13 <= $hour && 15 > $hour) {
  215. $period = '响午';
  216. } elseif (15 <= $hour && 18 > $hour) {
  217. $period = '下午';
  218. } elseif (18 <= $hour && 20 > $hour) {
  219. $period = '傍晚';
  220. } elseif (20 <= $hour && 22 > $hour) {
  221. $period = '晚上';
  222. } elseif (22 <= $hour && 23 >= $hour) {
  223. $period = '深夜';
  224. }
  225. return $period;
  226. }
  227. /**
  228. * 获取UTC日期格式
  229. *
  230. * @param mixed $dateTime 时间,默认为null则获取当前时间
  231. * @return string
  232. */
  233. public static function getUTCDate($dateTime = null) {
  234. $oldTimezone = self::getTimezone();
  235. if ('UTC' !== strtoupper($oldTimezone)) {
  236. self::setTimezone('UTC');
  237. }
  238. $date = date('D, d M y H:i:s e', self::getTimeStamp($dateTime));
  239. if ('UTC' !== strtoupper($oldTimezone)) {
  240. self::setTimezone($oldTimezone);
  241. }
  242. return $date;
  243. }
  244. /**
  245. * 获取微秒数
  246. *
  247. * @param string $mircrotime 微妙时间,默认为null则获取当前时间
  248. * @param string $get_as_float 获取微妙时间是否以浮点数返回,默认为false即不以浮点数方式返回
  249. * @return int
  250. */
  251. public static function getMicroTime($mircrotime = null, $get_as_float = false) {
  252. return array_sum(explode(' ', $mircrotime ? $mircrotime : microtime($get_as_float)));
  253. }
  254. /**
  255. * 判断是否是闰年
  256. *
  257. * @param int $year 需要判断的年份
  258. * @return boolean 如果是润年则返回true
  259. */
  260. public static function isLeapYear($year) {
  261. return (0 == $year % 4 && 0 != $year % 100 || 0 == $year % 400);
  262. }
  263. /**
  264. * 获得时间戳
  265. *
  266. * @param int $dateTime 时间戳,默认为null则以当前时间戳返回
  267. * @return int
  268. */
  269. public static function getTimeStamp($dateTime = null) {
  270. return $dateTime ? is_int($dateTime) ? $dateTime : strtotime($dateTime) : time();
  271. }
  272. /**
  273. * 比较两个时间返回离现在最近的一个时间
  274. *
  275. * @param int $time 当前时间戳
  276. * @param int $timestamp 比较的时间戳,默认为null则获取当前时间戳
  277. * @param string $format 格式化当前时间戳,默认为null则转化为格式Y-m-d H:i:s
  278. * @param array $type 要返回的时间类型,默认为 1则只返回Y-m-d否则返回Y-m-d m-d H:i
  279. * @return array
  280. */
  281. public static function getLastDate($time, $timestamp = null, $format = null, $type = 1) {
  282. $timelang = array('second' => '秒前', 'yesterday' => '昨天', 'hour' => '小时前', 'minute' => '分钟前', 'qiantian' => '前天');
  283. $timestamp = $timestamp ? $timestamp : time();
  284. $compareTime = strtotime(self::format('Y-m-d', $timestamp));
  285. $currentTime = strtotime(self::format('Y-m-d', $time));
  286. $decrease = $timestamp - $time;
  287. $result = self::format($format, $time);
  288. if (0 >= $decrease) {
  289. return 1 == $type ? array(self::format('Y-m-d', $time), $result) : array(
  290. self::format('Y-m-d m-d H:i', $time), $result);
  291. }
  292. if ($currentTime == $compareTime) {
  293. if (1 == $type) {
  294. if (60 >= $decrease) {
  295. return array($decrease . $timelang['second'], $result);
  296. }
  297. return 3600 >= $decrease ? array(ceil($decrease / 60) . $timelang['minute'], $result) : array(
  298. ceil($decrease / 3600) . $timelang['hour'], $result);
  299. }
  300. return array(self::format('H:i', $time), $result);
  301. } elseif ($currentTime == $compareTime - 86400) {
  302. return 1 == $type ? array($timelang['yesterday'] . " " . self::format('H:i', $time), $result) : array(
  303. self::format('m-d H:i', $time), $result);
  304. } elseif ($currentTime == $compareTime - 172800) {
  305. return 1 == $type ? array($timelang['qiantian'] . " " . self::format('H:i', $time), $result) : array(
  306. self::format('m-d H:i', $time), $result);
  307. } elseif (strtotime(self::format('Y', $time)) == strtotime(self::format('Y', $timestamp))) {
  308. return 1 == $type ? array(self::format('m-d', $time), $result) : array(self::format('m-d H:i', $time),
  309. $result);
  310. }
  311. return 1 == $type ? array(self::format('Y-m-d', $time), $result) : array(self::format('Y-m-d m-d H:i', $time),
  312. $result);
  313. }
  314. }