TimeUtil.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. var_dump($fmt);
  151. my_Assert(strtolower($fmt) == 'h', '获取Hour的参数只接受: H/h');
  152. return date($fmt);
  153. }
  154. }
  155. /**
  156. * 当前时间戳
  157. * @param int $offset 偏移量(秒)
  158. * @return int
  159. */
  160. function now($offset = 0) {
  161. return TimeUtil::tsCurrent($offset);
  162. }
  163. /**
  164. * 天(自1970年7月1号以来)的整数
  165. * @param int $time Unix Timestamp
  166. * @return int
  167. */
  168. function tsDay($time = -1) {
  169. return TimeUtil::tsDay($time);
  170. }
  171. /**
  172. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的微秒数。
  173. * @return float
  174. */
  175. function microsecond() {
  176. return microtime(true) * 1000 * 1000;
  177. }
  178. /**
  179. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的毫秒数。
  180. * @return float
  181. */
  182. function millisecond() {
  183. return microtime(true) * 1000;
  184. }
  185. /**
  186. * 渲染计时器
  187. *
  188. */
  189. class RenderTime {
  190. private $start;
  191. private $end;
  192. public function __construct() {
  193. $this->start();
  194. }
  195. /**
  196. * 开始计时
  197. */
  198. public function start() {
  199. $this->start = microsecond();
  200. $this->end = null;
  201. }
  202. /**
  203. * 停止计时
  204. */
  205. public function end() {
  206. $this->end = microsecond();
  207. }
  208. /**
  209. * 返回start和end之间消耗的时间
  210. * @param boolean $showUnit 是否附加单位(ss)字符串
  211. * @return int start和end见消逝的微秒数. <br/>
  212. */
  213. public function getRenderTime($showUnit = true) {
  214. $test = is_float($this->start) && is_float($this->end) && $this->start <= $this->end;
  215. if ($test) {
  216. $duration = round(($this->end - $this->start), 5); # 精确到小数点后5位
  217. if ($showUnit) { # 附加显示单位字符串
  218. return number_format($duration) . '微秒. ';
  219. } else {
  220. return $duration;
  221. }
  222. } else {
  223. return 'Can\'t calc the render time!';
  224. }
  225. }
  226. /**
  227. * 测试
  228. * @param callable $func 待测试代码
  229. * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经查挂.o(╯□╰)o
  230. */
  231. public static function Test($func, $times = 1000) {
  232. if (is_callable($func)) {
  233. $n = intval($times); # 防御错误的输入
  234. $t = new RenderTime();
  235. for ($i = 0; $i < $n; $i++) {
  236. $func();
  237. }
  238. $t->end();
  239. echoLine('memory: ' . number_format(memory_get_peak_usage() / 1024)
  240. . "kb, looped: $i, timesp: " . $t->getRenderTime());
  241. } else {
  242. echoLine("$func is not callable!");
  243. }
  244. }
  245. }
  246. /**
  247. * 测试
  248. * @param callable $func 待测试代码
  249. * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经常挂.o(╯□╰)o
  250. */
  251. function test($func, $times = 1000) {
  252. RenderTime::Test($func, $times);
  253. }