TimeUtil.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. * 当前时间戳
  238. * @param int $offset 偏移量(秒)
  239. * @return int
  240. */
  241. function now($offset = 0) {
  242. return TimeUtil::tsCurrent($offset);
  243. }
  244. /**
  245. * 天(自1970年7月1号以来)的整数
  246. * @param int $time Unix Timestamp
  247. * @deprecated since version 建议使用变更后的名称 totalDays()
  248. * @return int
  249. */
  250. function tsDay($time = -1) {
  251. return TimeUtil::totalDays($time);
  252. }
  253. /**
  254. * 天(自1970年7月1号以来)的整数
  255. * @param int $time Unix Timestamp 指定时刻的时间戳
  256. * @return int
  257. */
  258. function totalDays($time = -1) {
  259. return TimeUtil::totalDays($time);
  260. }
  261. /**
  262. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的微秒数。
  263. * @return float
  264. */
  265. function microsecond() {
  266. return round(microtime(true) * 1000 * 1000);
  267. }
  268. /**
  269. * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的毫秒数。
  270. * @return float
  271. */
  272. function millisecond() {
  273. return round(microtime(true) * 1000);
  274. }
  275. /**
  276. * 渲染计时器
  277. *
  278. */
  279. class RenderTime {
  280. private $start;
  281. private $end;
  282. public function __construct() {
  283. $this->start();
  284. }
  285. /**
  286. * 开始计时
  287. */
  288. public function start() {
  289. $this->start = microsecond();
  290. $this->end = null;
  291. }
  292. /**
  293. * 停止计时
  294. */
  295. public function end() {
  296. $this->end = microsecond();
  297. }
  298. /**
  299. * 返回start和end之间消耗的时间
  300. * @param boolean $showUnit 是否附加单位(ss)字符串
  301. * @return int start和end见消逝的微秒数. <br/>
  302. */
  303. public function getRenderTime($showUnit = true) {
  304. $test = is_float($this->start) && is_float($this->end) && $this->start <= $this->end;
  305. if ($test) {
  306. $duration = round(($this->end - $this->start), 5); # 精确到小数点后5位
  307. if ($showUnit) { # 附加显示单位字符串
  308. return number_format($duration) . '微秒. ';
  309. } else {
  310. return $duration;
  311. }
  312. } else {
  313. return 'Can\'t calc the render time!';
  314. }
  315. }
  316. /**
  317. * 测试
  318. * @param callable $func 待测试代码
  319. * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经查挂.o(╯□╰)o
  320. */
  321. public static function Test($func, $times = 1000) {
  322. if (is_callable($func)) {
  323. $n = intval($times); # 防御错误的输入
  324. $t = new RenderTime();
  325. for ($i = 0; $i < $n; $i++) {
  326. $func();
  327. }
  328. $t->end();
  329. echoLine('memory: ' . number_format(memory_get_peak_usage() / 1024)
  330. . "kb, looped: $i, timesp: " . $t->getRenderTime());
  331. } else {
  332. echoLine("$func is not callable!");
  333. }
  334. }
  335. }
  336. /**
  337. * 测试
  338. * @param callable $func 待测试代码
  339. * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经常挂.o(╯□╰)o
  340. */
  341. function test($func, $times = 1000) {
  342. RenderTime::Test($func, $times);
  343. }