TimeUtil.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * 时间功能辅助类
  5. */
  6. class TimeUtil
  7. {
  8. /**
  9. * 获取服务端的本地时间戳
  10. * 客户端时间要以服务端的为准.
  11. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。
  12. * @param type $offset 时间偏移:用于某些服务端有必要出现时间偏差的情况
  13. * @return int
  14. */
  15. public static function tsCurrent($offset = 0)
  16. {
  17. return time() + $offset;
  18. }
  19. /**
  20. * 代表无限期以后(大概是2033年)的时间戳
  21. * @return int 1999999999
  22. */
  23. public static function tsNoEnd()
  24. {
  25. return 1999999999;
  26. }
  27. /**
  28. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的天数。
  29. * 检测玩家连续登录及隔天刷新操作用
  30. * @param type $time 当前时间戳
  31. * @return type
  32. */
  33. public static function tsDay($time = -1)
  34. {
  35. if ($time < 0) {
  36. $time = TimeUtil::tsCurrent();
  37. }
  38. return CommUtil::floatToInt(($time + 28800) / 86400); # 东八区前提8小时时差
  39. }
  40. /**
  41. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的周数。
  42. * @param int $time ts时间戳
  43. * @return number
  44. */
  45. public static function tsWeek($time = -1)
  46. {
  47. return CommUtil::floatToInt((TimeUtil::tsDay($time) + 3) / 7);
  48. }
  49. /**
  50. * 返回年月日时分秒(例:20161028101530)这样的时间戳
  51. * @return number
  52. */
  53. public static function tsYmdHis($fmt = "YmdHis")
  54. {
  55. return date($fmt);
  56. }
  57. /**
  58. * 获取当前的日期时间(例: 2016-06-16 18:08:18 )字符串
  59. * date('Y-m-d H:i:s')
  60. * @return string
  61. */
  62. public static function dtCurrent($fmt = 'Y-m-d H:i:s')
  63. {
  64. return date($fmt);
  65. }
  66. /**
  67. * 获取当前日期, 默认格式20160702
  68. * @param string $format 可以自定义输出格式, 参考date(format)
  69. */
  70. public static function dtToday($format = 'Ymd')
  71. {
  72. return date($format);
  73. }
  74. /**
  75. * 获取昨天的日期默认格式 20160702
  76. * @param string $format 可以自定义输出格式参考 date(format)
  77. * @return string
  78. */
  79. public static function dtYesterday($format = 'Ymd')
  80. {
  81. return date($format, strtotime("-1 day"));
  82. }
  83. /**
  84. * 获取明天的日期, 默认格式20160702
  85. * @param string $format 可以自定义输出格式, 参考date(format)
  86. * @return string
  87. */
  88. public static function dtTomorrow($format = 'Ymd')
  89. {
  90. return date($format, strtotime("+1 day"));
  91. }
  92. /**
  93. * 获取上周x的日期, 默认格式20160702
  94. * @param string $format 可以自定义输出格式, 参考date(format)
  95. * @return string
  96. */
  97. public static function dtLastWeek($format = 'Ymd')
  98. {
  99. return date($format, strtotime("-1 week"));
  100. }
  101. /**
  102. * 获取下周x的日期,默认格式20160702
  103. * @param string $format 可以自定义输出格式, 参考date(format)
  104. * @return string
  105. */
  106. public static function dtNextWeek($format = 'Ymd')
  107. {
  108. return date($format, strtotime("+1 week"));
  109. }
  110. /**
  111. * 获取30天之前的日期, 默认格式20160702
  112. * @param string $format 可以自定义输出格式, 参考date(format)
  113. * @return string
  114. */
  115. public static function dtLastMonth($format = 'Ymd')
  116. {
  117. return date($format, strtotime("-30 day"));
  118. }
  119. /**
  120. * 获取30天之后的日期, 默认格式20160702
  121. * @param string $format 可以自定义输出格式, 参考date(format)
  122. * @return string
  123. */
  124. public static function dtNextMonth($format = 'Ymd')
  125. {
  126. return date($format, strtotime("+30 day"));
  127. }
  128. /**
  129. * 获取90天之后的日期, 默认格式20160702
  130. * @param string $format 可以自定义输出格式, 参考date(format)
  131. * @return string
  132. */
  133. public static function dtNextThreeMonth($format = 'Ymd')
  134. {
  135. return date($format, strtotime("+90 day"));
  136. }
  137. /**
  138. * 获取90天之前的日期, 默认格式20160702
  139. * @param string $format 可以自定义输出格式, 参考date(format)
  140. * @return string
  141. */
  142. public static function dtLastThreeMonth($format = 'Ymd')
  143. {
  144. return date($format, strtotime("-90 day"));
  145. }
  146. /**
  147. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的微秒数。
  148. * @return float 注意这个值的精度, 需要使用高精度数据类型保存和使用
  149. */
  150. public static function microsecond()
  151. {
  152. return microtime(true) * 1000 * 1000;
  153. }
  154. /**
  155. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的毫秒数。
  156. * @return float 注意这个值的精度, 需要使用高精度数据类型保存和使用
  157. */
  158. public static function millisecond()
  159. {
  160. return microtime(true) * 1000;
  161. }
  162. }
  163. /**
  164. * 当前时间戳
  165. * @param int $offset 偏移量(秒)
  166. * @return int
  167. */
  168. function now($offset = 0)
  169. {
  170. return TimeUtil::tsCurrent($offset);
  171. }
  172. /**
  173. * 天(自1970年7月1号以来)的整数
  174. * @param int $time Unix Timestamp
  175. * @return int
  176. */
  177. function tsDay($time = -1)
  178. {
  179. return TimeUtil::tsDay($time);
  180. }
  181. /**
  182. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的微秒数。
  183. * @return float
  184. */
  185. function microsecond()
  186. {
  187. return microtime(true) * 1000 * 1000;
  188. }
  189. /**
  190. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的毫秒数。
  191. * @return float
  192. */
  193. function millisecond()
  194. {
  195. return microtime(true) * 1000;
  196. }
  197. /**
  198. * 渲染计时器
  199. *
  200. */
  201. class RenderTime
  202. {
  203. private $start;
  204. private $end;
  205. public function __construct()
  206. {
  207. $this->start();
  208. }
  209. /**
  210. * 开始计时
  211. */
  212. public function start()
  213. {
  214. $this->start = microsecond();
  215. $this->end = null;
  216. }
  217. /**
  218. * 停止计时
  219. */
  220. public function end()
  221. {
  222. $this->end = microsecond();
  223. }
  224. /**
  225. * 返回start和end之间消耗的时间
  226. * @param boolean $showUnit 是否附加单位(ss)字符串
  227. * @return int start和end见消逝的微秒数. <br/>
  228. */
  229. public function getRenderTime($showUnit = true)
  230. {
  231. $test = is_float($this->start) && is_float($this->end) && $this->start <= $this->end;
  232. if ($test) {
  233. $duration = round(($this->end - $this->start), 5); # 精确到小数点后5位
  234. if ($showUnit) { # 附加显示单位字符串
  235. return number_format($duration) . '微秒. ';
  236. } else {
  237. return $duration;
  238. }
  239. } else {
  240. return 'Can\'t calc the render time!';
  241. }
  242. }
  243. /**
  244. * 测试
  245. * @param callable $func 待测试代码
  246. * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经查挂.o(╯□╰)o
  247. */
  248. public static function Test($func, $times = 1000)
  249. {
  250. if (is_callable($func)) {
  251. $n = intval($times); # 防御错误的输入
  252. $t = new RenderTime();
  253. for ($i = 0; $i < $n; $i++) {
  254. $func();
  255. }
  256. $t->end();
  257. echoLine('memory: ' . number_format(memory_get_peak_usage() / 1024)
  258. . "kb, looped: $i, timesp: " . $t->getRenderTime());
  259. } else {
  260. echoLine("$func is not callable!");
  261. }
  262. }
  263. }
  264. /**
  265. * 测试
  266. * @param callable $func 待测试代码
  267. * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经常挂.o(╯□╰)o
  268. */
  269. function test($func, $times = 1000)
  270. {
  271. RenderTime::Test($func, $times);
  272. }