123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- namespace loyalsoft;
- /**
- * 时间功能辅助类
- */
- class TimeUtil {
- /**
- * 获取服务端的本地时间戳
- * 客户端时间要以服务端的为准.
- * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。
- * @param type $offset 时间偏移:用于某些服务端有必要出现时间偏差的情况
- * @return int
- */
- public static function tsCurrent($offset = 0) {
- return time() + $offset;
- }
- /**
- * 代表无限期以后(大概是2033年)的时间戳
- * @return int 1999999999
- */
- public static function tsNoEnd() {
- return 1999999999;
- }
- /**
- * 计算某时刻所在日的开始时间戳
- * @param type $time
- * @return type
- */
- public static function tsDayBegin($time = -1) {
- if ($time < 0) {
- $time = TimeUtil::tsCurrent();
- }
- $year = date("Y", $time);
- $month = date("m", $time);
- $day = date("d", $time);
- return mktime(0, 0, 0, $month, $day, $year); # 创建当日开始时间戳
- }
- /**
- * 计算某时刻所在日的最后一秒时间戳
- * @param type $time
- * @return type
- */
- public static function tsDayEnd($time = -1) {
- if ($time < 0) {
- $time = TimeUtil::tsCurrent();
- }
- $year = date("Y", $time);
- $month = date("m", $time);
- $day = date("d", $time);
- return mktime(23, 59, 59, $month, $day, $year); # 创建当日最后一秒时间戳
- }
- /**
- * 计算某时刻所在月份的开始时间戳
- * @param type $time
- * @return type
- */
- public static function tsMonthBegin($time = -1) {
- if ($time < 0) {
- $time = TimeUtil::tsCurrent();
- }
- $year = date("Y", $time);
- $month = date("m", $time);
- return mktime(0, 0, 0, $month, 1, $year); # 创建当月开始时间戳
- }
- /**
- * 计算某时刻所在月份的最后一秒时间戳
- * @param type $time
- * @return type
- */
- public static function tsMonthEnd($time = -1) {
- if ($time < 0) {
- $time = TimeUtil::tsCurrent();
- }
- $year = date("Y", $time);
- $month = date("m", $time);
- $t = date('t'); # 该月一共有几天
- return mktime(23, 59, 59, $month, $t, $year); # 创建当月最后一秒的时间戳
- }
- // <editor-fold defaultstate="collapsed" desc="累计天数/周数">
- /**
- * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的周数。
- * @param int $time ts时间戳
- * @return number
- */
- public static function totalWeeks($time = -1) {
- return CommUtil::floatToInt((TimeUtil::totalDays($time) + 3) / 7);
- }
- /**
- * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的天数。
- * 检测玩家连续登录及隔天刷新操作用
- * @param type $time 当前时间戳
- * @return type
- */
- public static function totalDays($time = -1) {
- if ($time < 0) {
- $time = TimeUtil::tsCurrent();
- }
- return CommUtil::floatToInt(($time + 28800) / 86400); # 东八区前提8小时时差
- }
- /**
- * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的天数。
- * 检测玩家连续登录及隔天刷新操作用
- * @deprecated since version 请使用变更后的名称 totalDays()
- * @param type $time 当前时间戳
- * @return type
- */
- public static function tsDay($time = -1) {
- if ($time < 0) {
- $time = TimeUtil::tsCurrent();
- }
- return CommUtil::floatToInt(($time + 28800) / 86400); # 东八区前提8小时时差
- }
- // </editor-fold>
- /**
- * 返回年月日时分秒(例:20161028101530)这样的时间戳
- * @return number
- */
- public static function tsYmdHis($fmt = "YmdHis") {
- return date($fmt);
- }
- //
- // <editor-fold defaultstate="collapsed" desc="快速获得日期字符串">
- /**
- * 返回年月日时分秒(例:20161028101530)这样的时间格式
- * @return number
- */
- public static function dtYmdHis($fmt = "YmdHis") {
- return date($fmt);
- }
- /**
- * 获取当前的日期时间(例: 2016-06-16 18:08:18 )字符串
- * date('Y-m-d H:i:s')
- * @return string
- */
- public static function dtCurrent($fmt = 'Y-m-d H:i:s') {
- return date($fmt);
- }
- /**
- * 获取当前日期, 默认格式20160702
- * @param string $format 可以自定义输出格式, 参考date(format)
- */
- public static function dtToday($format = 'Ymd') {
- return date($format);
- }
- /**
- * 获取昨天的日期默认格式 20160702
- * @param string $format 可以自定义输出格式参考 date(format)
- * @return string
- */
- public static function dtYesterday($format = 'Ymd') {
- return date($format, strtotime("-1 day"));
- }
- /**
- * 获取明天的日期, 默认格式20160702
- * @param string $format 可以自定义输出格式, 参考date(format)
- * @return string
- */
- public static function dtTomorrow($format = 'Ymd') {
- return date($format, strtotime("+1 day"));
- }
- /**
- * 获取上周x的日期, 默认格式20160702
- * @param string $format 可以自定义输出格式, 参考date(format)
- * @return string
- */
- public static function dtLastWeek($format = 'Ymd') {
- return date($format, strtotime("-1 week"));
- }
- /**
- * 获取下周x的日期,默认格式20160702
- * @param string $format 可以自定义输出格式, 参考date(format)
- * @return string
- */
- public static function dtNextWeek($format = 'Ymd') {
- return date($format, strtotime("+1 week"));
- }
- /**
- * 获取30天之前的日期, 默认格式20160702
- * @param string $format 可以自定义输出格式, 参考date(format)
- * @return string
- */
- public static function dtLastMonth($format = 'Ymd') {
- return date($format, strtotime("-30 day"));
- }
- /**
- * 获取30天之后的日期, 默认格式20160702
- * @param string $format 可以自定义输出格式, 参考date(format)
- * @return string
- */
- public static function dtNextMonth($format = 'Ymd') {
- return date($format, strtotime("+30 day"));
- }
- /**
- * 获取90天之后的日期, 默认格式20160702
- * @param string $format 可以自定义输出格式, 参考date(format)
- * @return string
- */
- public static function dtNextThreeMonth($format = 'Ymd') {
- return date($format, strtotime("+90 day"));
- }
- /**
- * 获取90天之前的日期, 默认格式20160702
- * @param string $format 可以自定义输出格式, 参考date(format)
- * @return string
- */
- public static function dtLastThreeMonth($format = 'Ymd') {
- return date($format, strtotime("-90 day"));
- }
- // </editor-fold>
- //
- /**
- * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的微秒数。
- * @return float 注意这个值的精度, 需要使用高精度数据类型保存和使用
- */
- public static function microsecond() {
- return microtime(true) * 1000 * 1000;
- }
- /**
- * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的毫秒数。
- * @return float 注意这个值的精度, 需要使用高精度数据类型保存和使用
- */
- public static function millisecond() {
- return microtime(true) * 1000;
- }
- /**
- * 计算指定时刻(默认当前)的小时数字
- * @param type $ts 时间戳, 默认-1,采用当前时间
- * @param string $fmt 默认'H'(24小时制),可以输入'h'
- * @return type
- */
- public static function Hour($ts = -1, $fmt = 'H') {
- my_Assert(strtolower($fmt) == 'h', '获取Hour的参数只接受: H/h');
- return date($fmt, $ts);
- }
- /**
- * 隔几天一刷新,获取上次刷新的时间戳 以便计算下次隔几天该刷新了
- * @param type $startTs
- * @param type $day
- */
- public static function getNextDayTs($startTs, $day) {
- $startDay = TimeUtil::totalDays($startTs);
- $nextDay = TimeUtil::totalDays();
- $num = $nextDay - $startDay;
- $refershType = $num % $day; //day 2天一刷新 余数只会是0 或 1; 0:两天后在刷新,1就是已经过了一天了,在过一天就刷新,当前时间减一天
- return now() - 86400 * $refershType;
- }
- }
- /**
- * 当前时间戳
- * @param int $offset 偏移量(秒)
- * @return int
- */
- function now($offset = 0) {
- return TimeUtil::tsCurrent($offset);
- }
- /**
- * 天(自1970年7月1号以来)的整数
- * @param int $time Unix Timestamp
- * @deprecated since version 建议使用变更后的名称 totalDays()
- * @return int
- */
- function tsDay($time = -1) {
- return TimeUtil::totalDays($time);
- }
- /**
- * 天(自1970年7月1号以来)的整数
- * @param int $time Unix Timestamp 指定时刻的时间戳
- * @return int
- */
- function totalDays($time = -1) {
- return TimeUtil::totalDays($time);
- }
- /**
- * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的微秒数。
- * @return float
- */
- function microsecond() {
- return round(microtime(true) * 1000 * 1000);
- }
- /**
- * 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的毫秒数。
- * @return float
- */
- function millisecond() {
- return round(microtime(true) * 1000);
- }
- /**
- * 渲染计时器
- *
- */
- class RenderTime {
- private $start;
- private $end;
- public function __construct() {
- $this->start();
- }
- /**
- * 开始计时
- */
- public function start() {
- $this->start = microsecond();
- $this->end = null;
- }
- /**
- * 停止计时
- */
- public function end() {
- $this->end = microsecond();
- }
- /**
- * 返回start和end之间消耗的时间
- * @param boolean $showUnit 是否附加单位(ss)字符串
- * @return int start和end见消逝的微秒数. <br/>
- */
- public function getRenderTime($showUnit = true) {
- $test = is_float($this->start) && is_float($this->end) && $this->start <= $this->end;
- if ($test) {
- $duration = round(($this->end - $this->start), 5); # 精确到小数点后5位
- if ($showUnit) { # 附加显示单位字符串
- return number_format($duration) . '微秒. ';
- } else {
- return $duration;
- }
- } else {
- return 'Can\'t calc the render time!';
- }
- }
- /**
- * 测试
- * @param callable $func 待测试代码
- * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经查挂.o(╯□╰)o
- */
- public static function Test($func, $times = 1000) {
- if (is_callable($func)) {
- $n = intval($times); # 防御错误的输入
- $t = new RenderTime();
- for ($i = 0; $i < $n; $i++) {
- $func();
- }
- $t->end();
- echoLine('memory: ' . number_format(memory_get_peak_usage() / 1024)
- . "kb, looped: $i, timesp: " . $t->getRenderTime());
- } else {
- echoLine("$func is not callable!");
- }
- }
- }
- /**
- * 测试
- * @param callable $func 待测试代码
- * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经常挂.o(╯□╰)o
- */
- function test($func, $times = 1000) {
- RenderTime::Test($func, $times);
- }
|