/**
* 返回自从 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小时时差
}
//
/**
* 返回年月日时分秒(例:20161028101530)这样的时间戳
* @return number
*/
public static function tsYmdHis($fmt = "YmdHis") {
return date($fmt);
}
//
//
/**
* 返回年月日时分秒(例: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"));
}
//
//
/**
* 返回自从 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见消逝的微秒数.
*/
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);
}