TimeUtil.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * 时间功能辅助类
  5. */
  6. class TimeUtil {
  7. /**
  8. * 获取服务端的本地时间戳
  9. * 客户端时间要以服务端的为准.
  10. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。
  11. * @param type $offset 时间偏移:用于某些服务端有必要出现时间偏差的情况
  12. * @return int
  13. */
  14. public static function tsCurrent($offset = 0) {
  15. return time() + $offset;
  16. }
  17. /**
  18. * 代表无限期以后(大概是2033年)的时间戳
  19. * @return int 1999999999
  20. */
  21. public static function tsNoEnd() {
  22. return 1999999999;
  23. }
  24. /**
  25. * 计算某时刻所在日的开始时间戳
  26. * @param type $time
  27. * @return type
  28. */
  29. public static function tsDayBegin($time = -1) {
  30. if ($time < 0) {
  31. $time = TimeUtil::tsCurrent();
  32. }
  33. $year = date("Y", $time);
  34. $month = date("m", $time);
  35. $day = date("d", $time);
  36. return mktime(0, 0, 0, $month, $day, $year); # 创建当日开始时间戳
  37. }
  38. /**
  39. * 计算某时刻所在日的最后一秒时间戳
  40. * @param type $time
  41. * @return type
  42. */
  43. public static function tsDayEnd($time = -1) {
  44. if ($time < 0) {
  45. $time = TimeUtil::tsCurrent();
  46. }
  47. $year = date("Y", $time);
  48. $month = date("m", $time);
  49. $day = date("d", $time);
  50. return mktime(23, 59, 59, $month, $day, $year); # 创建当日最后一秒时间戳
  51. }
  52. /**
  53. * 计算某时刻所在月份的开始时间戳
  54. * @param type $time
  55. * @return type
  56. */
  57. public static function tsMonthBegin($time = -1) {
  58. if ($time < 0) {
  59. $time = TimeUtil::tsCurrent();
  60. }
  61. $year = date("Y", $time);
  62. $month = date("m", $time);
  63. return mktime(0, 0, 0, $month, 1, $year); # 创建当月开始时间戳
  64. }
  65. /**
  66. * 计算某时刻所在月份的最后一秒时间戳
  67. * @param type $time
  68. * @return type
  69. */
  70. public static function tsMonthEnd($time = -1) {
  71. if ($time < 0) {
  72. $time = TimeUtil::tsCurrent();
  73. }
  74. $year = date("Y", $time);
  75. $month = date("m", $time);
  76. $t = date('t'); # 该月一共有几天
  77. return mktime(23, 59, 59, $month, $t, $year); # 创建当月最后一秒的时间戳
  78. }
  79. // <editor-fold defaultstate="collapsed" desc="累计天数/周数">
  80. /**
  81. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的周数。
  82. * @param int $time ts时间戳
  83. * @return number
  84. */
  85. public static function totalWeeks($time = -1) {
  86. return CommUtil::floatToInt((TimeUtil::totalDays($time) + 3) / 7);
  87. }
  88. /**
  89. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的天数。
  90. * 检测玩家连续登录及隔天刷新操作用
  91. * @param type $time 当前时间戳
  92. * @return type
  93. */
  94. public static function totalDays($time = -1) {
  95. if ($time < 0) {
  96. $time = TimeUtil::tsCurrent();
  97. }
  98. return CommUtil::floatToInt(($time + 28800) / 86400); # 东八区前提8小时时差
  99. }
  100. /**
  101. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的天数。
  102. * 检测玩家连续登录及隔天刷新操作用
  103. * @deprecated since version 请使用变更后的名称 totalDays()
  104. * @param type $time 当前时间戳
  105. * @return type
  106. */
  107. public static function tsDay($time = -1) {
  108. if ($time < 0) {
  109. $time = TimeUtil::tsCurrent();
  110. }
  111. return CommUtil::floatToInt(($time + 28800) / 86400); # 东八区前提8小时时差
  112. }
  113. // </editor-fold>
  114. /**
  115. * 返回年月日时分秒(例:20161028101530)这样的时间戳
  116. * @return number
  117. */
  118. public static function tsYmdHis($fmt = "YmdHis") {
  119. return date($fmt);
  120. }
  121. //
  122. // <editor-fold defaultstate="collapsed" desc="快速获得日期字符串">
  123. /**
  124. * 返回年月日时分秒(例:20161028101530)这样的时间格式
  125. * @return number
  126. */
  127. public static function dtYmdHis($fmt = "YmdHis") {
  128. return date($fmt);
  129. }
  130. /**
  131. * 获取当前的日期时间(例: 2016-06-16 18:08:18 )字符串
  132. * date('Y-m-d H:i:s')
  133. * @return string
  134. */
  135. public static function dtCurrent($fmt = 'Y-m-d H:i:s') {
  136. return date($fmt);
  137. }
  138. /**
  139. * 获取当前日期, 默认格式20160702
  140. * @param string $format 可以自定义输出格式, 参考date(format)
  141. */
  142. public static function dtToday($format = 'Ymd') {
  143. return date($format);
  144. }
  145. /**
  146. * 获取昨天的日期默认格式 20160702
  147. * @param string $format 可以自定义输出格式参考 date(format)
  148. * @return string
  149. */
  150. public static function dtYesterday($format = 'Ymd') {
  151. return date($format, strtotime("-1 day"));
  152. }
  153. /**
  154. * 获取明天的日期, 默认格式20160702
  155. * @param string $format 可以自定义输出格式, 参考date(format)
  156. * @return string
  157. */
  158. public static function dtTomorrow($format = 'Ymd') {
  159. return date($format, strtotime("+1 day"));
  160. }
  161. /**
  162. * 获取上周x的日期, 默认格式20160702
  163. * @param string $format 可以自定义输出格式, 参考date(format)
  164. * @return string
  165. */
  166. public static function dtLastWeek($format = 'Ymd') {
  167. return date($format, strtotime("-1 week"));
  168. }
  169. /**
  170. * 获取下周x的日期,默认格式20160702
  171. * @param string $format 可以自定义输出格式, 参考date(format)
  172. * @return string
  173. */
  174. public static function dtNextWeek($format = 'Ymd') {
  175. return date($format, strtotime("+1 week"));
  176. }
  177. /**
  178. * 获取30天之前的日期, 默认格式20160702
  179. * @param string $format 可以自定义输出格式, 参考date(format)
  180. * @return string
  181. */
  182. public static function dtLastMonth($format = 'Ymd') {
  183. return date($format, strtotime("-30 day"));
  184. }
  185. /**
  186. * 获取30天之后的日期, 默认格式20160702
  187. * @param string $format 可以自定义输出格式, 参考date(format)
  188. * @return string
  189. */
  190. public static function dtNextMonth($format = 'Ymd') {
  191. return date($format, strtotime("+30 day"));
  192. }
  193. /**
  194. * 获取90天之后的日期, 默认格式20160702
  195. * @param string $format 可以自定义输出格式, 参考date(format)
  196. * @return string
  197. */
  198. public static function dtNextThreeMonth($format = 'Ymd') {
  199. return date($format, strtotime("+90 day"));
  200. }
  201. /**
  202. * 获取90天之前的日期, 默认格式20160702
  203. * @param string $format 可以自定义输出格式, 参考date(format)
  204. * @return string
  205. */
  206. public static function dtLastThreeMonth($format = 'Ymd') {
  207. return date($format, strtotime("-90 day"));
  208. }
  209. // </editor-fold>
  210. //
  211. /**
  212. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的微秒数。
  213. * @return float 注意这个值的精度, 需要使用高精度数据类型保存和使用
  214. */
  215. public static function microsecond() {
  216. return microtime(true) * 1000 * 1000;
  217. }
  218. /**
  219. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的毫秒数。
  220. * @return float 注意这个值的精度, 需要使用高精度数据类型保存和使用
  221. */
  222. public static function millisecond() {
  223. return microtime(true) * 1000;
  224. }
  225. /**
  226. * 计算指定时刻(默认当前)的小时数字
  227. * @param type $ts 时间戳, 默认-1,采用当前时间
  228. * @param string $fmt 默认'H'(24小时制),可以输入'h'
  229. * @return type
  230. */
  231. public static function Hour($ts = -1, $fmt = 'H') {
  232. my_Assert(strtolower($fmt) == 'h', '获取Hour的参数只接受: H/h');
  233. return date($fmt, $ts);
  234. }
  235. /**
  236. * 隔几天一刷新,获取上次刷新的时间戳 以便计算下次隔几天该刷新了
  237. * @param type $startTs
  238. * @param type $day
  239. */
  240. public static function getNextDayTs($startTs, $day) {
  241. $startDay = TimeUtil::totalDays($startTs);
  242. $nextDay = TimeUtil::totalDays();
  243. $num = $nextDay - $startDay;
  244. $refershType = $num % $day; //day 2天一刷新 余数只会是0 或 1; 0:两天后在刷新,1就是已经过了一天了,在过一天就刷新,当前时间减一天
  245. return now() - 86400 * $refershType;
  246. }
  247. }
  248. /**
  249. * 当前时间戳
  250. * @param int $offset 偏移量(秒)
  251. * @return int
  252. */
  253. function now($offset = 0) {
  254. return TimeUtil::tsCurrent($offset);
  255. }
  256. /**
  257. * 天(自1970年7月1号以来)的整数
  258. * @param int $time Unix Timestamp
  259. * @deprecated since version 建议使用变更后的名称 totalDays()
  260. * @return int
  261. */
  262. function tsDay($time = -1) {
  263. return TimeUtil::totalDays($time);
  264. }
  265. /**
  266. * 天(自1970年7月1号以来)的整数
  267. * @param int $time Unix Timestamp 指定时刻的时间戳
  268. * @return int
  269. */
  270. function totalDays($time = -1) {
  271. return TimeUtil::totalDays($time);
  272. }
  273. /**
  274. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的微秒数。
  275. * @return float
  276. */
  277. function microsecond() {
  278. return round(microtime(true) * 1000 * 1000);
  279. }
  280. /**
  281. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的毫秒数。
  282. * @return float
  283. */
  284. function millisecond() {
  285. return round(microtime(true) * 1000);
  286. }
  287. /**
  288. * 渲染计时器
  289. *
  290. */
  291. class RenderTime {
  292. private $start;
  293. private $end;
  294. public function __construct() {
  295. $this->start();
  296. }
  297. /**
  298. * 开始计时
  299. */
  300. public function start() {
  301. $this->start = microsecond();
  302. $this->end = null;
  303. }
  304. /**
  305. * 停止计时
  306. */
  307. public function end() {
  308. $this->end = microsecond();
  309. }
  310. /**
  311. * 返回start和end之间消耗的时间
  312. * @param boolean $showUnit 是否附加单位(ss)字符串
  313. * @return int start和end见消逝的微秒数. <br/>
  314. */
  315. public function getRenderTime($showUnit = true) {
  316. $test = is_float($this->start) && is_float($this->end) && $this->start <= $this->end;
  317. if ($test) {
  318. $duration = round(($this->end - $this->start), 5); # 精确到小数点后5位
  319. if ($showUnit) { # 附加显示单位字符串
  320. return number_format($duration) . '微秒. ';
  321. } else {
  322. return $duration;
  323. }
  324. } else {
  325. return 'Can\'t calc the render time!';
  326. }
  327. }
  328. /**
  329. * 测试
  330. * @param callable $func 待测试代码
  331. * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经查挂.o(╯□╰)o
  332. */
  333. public static function Test($func, $times = 1000) {
  334. if (is_callable($func)) {
  335. $n = intval($times); # 防御错误的输入
  336. $t = new RenderTime();
  337. for ($i = 0; $i < $n; $i++) {
  338. $func();
  339. }
  340. $t->end();
  341. echoLine('memory: ' . number_format(memory_get_peak_usage() / 1024)
  342. . "kb, looped: $i, timesp: " . $t->getRenderTime());
  343. } else {
  344. echoLine("$func is not callable!");
  345. }
  346. }
  347. }
  348. /**
  349. * 测试
  350. * @param callable $func 待测试代码
  351. * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经常挂.o(╯□╰)o
  352. */
  353. function test($func, $times = 1000) {
  354. RenderTime::Test($func, $times);
  355. }