php函数

复制代码
/**
 * 获取13位时间戳
 * @return float
 */
function getMillisecond()
{
    list($s1, $s2) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}
复制代码

 

复制代码
/**
 * 随机小数
 * @param float $min 最小值
 * @param float $max 最大值
 * @param int $precision 精准度
 * @return float
 */
function randFloat($min=0.1, $max=2.1 ,$precision=1){
    $fl=round($min + mt_rand()/mt_getrandmax() * ($max-$min),$precision);
    if (floor($fl)==$fl){
        return randFloat($min, $max);
    }else{
        return $fl;
    }
}
复制代码
复制代码
/*
 * 随机拆分
 * @param $total_num int 总数
 * @param $total_copies int 总份数
 * @return string/array
 * ps:将10拆成[1,7,3]
 */
function random_split($total_num,$total_copies){
    $result = []; //结果
    for($i=$total_copies;$i>0;$i--){
        $ls_num=0;
        $num = 0;
        if($total_num > 0){
            if($i==1){
                $num += $total_num;
            }else{
                $max_num = floor($total_num/$i);
                $ls_num = mt_rand(0,$max_num);
                $num += $ls_num;
            }
        }
        $result[] = $num;
        $total_num -= $ls_num;
    }
    shuffle($result); //打乱数组
    return $result; //返回数组
}
复制代码

 

复制代码
/**
 * php 获取今日、昨日、上周、本周、本月、上月、今年、最近一个月的起始时间戳和结束时间戳的方法
 */
//php获取今日开始时间戳和结束时间戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
//
$beginToday=strtotime(date('Y-m-d',strtotime('0 day')));
$endToday=strtotime(date('Y-m-d',strtotime('+1 day')));
//php获取昨日起始时间戳和结束时间戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
//
$beginYesterday=strtotime(date('Y-m-d',strtotime('-1 day')));
$endYesterday=strtotime(date('Y-m-d',strtotime('0 day')));
//php获取上周起始时间戳和结束时间戳【错误写法,当天为周日时以下写法错误,所以不能用】
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
//php获取上周起始时间戳和结束时间戳【正确写法】
$beginLastweek=strtotime("last week Monday",time());
$endLastweek=strtotime("last week Sunday",time())+86399;
//php获取本周起始时间戳和结束时间戳【开始时间周一,结束时间为周日】
$beginLastweek=strtotime("this week Monday",time());
$endLastweek=strtotime("this week Sunday",time())+86399;
//php获取本周起始时间戳和结束时间戳【开始时间周日,结束时间为周六】
$beginLastweek=mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
$endLastweek=mktime(0, 0 , 0,date("m"),date("d")-date("w")+7,date("Y"));
//php获取上一月起始时间戳和结束时间戳
$beginLastmonth=mktime(0, 0 , 0,date("m")-1,1,date("Y"));
$endLastmonth=mktime(23,59,59,date("m") ,0,date("Y"));
//php获取上上月起始时间戳和结束时间戳
$beginLastmonth2=mktime(0, 0 , 0,date("m")-2,1,date("Y"));
$endLastmonth2=mktime(23,59,59,date("m")-1 ,0,date("Y"));
//php获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
//php获取今年起始时间戳和结束时间戳
$beginThismonth=strtotime(date("Y",time())."-1"."-1");
$endThismonth=strtotime(date("Y",time())."-12"."-31");
复制代码

 

posted @   mingruqi  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示