TimeUtil.php 8.3 KB

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