PHP 常用时间差、时间戳、星期

获取两个时间点相差的天、时、分、秒:

   /**
     * 时间差
     * @param unknown $begin_time
     * @param unknown $end_time
     */
   function  timeDiff($begin_time, $end_time){
        if ( $begin_time < $end_time ) {
            $starttime = $begin_time;
            $endtime = $end_time;
        }
        else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        $timediff = $endtime - $starttime;
        $days = intval( $timediff / 86400 );
        $remain = $timediff % 86400;
        $hours = intval( $remain / 3600 );
        $remain = $remain % 3600;
        $mins = intval( $remain / 60 );
        $secs = $remain % 60;
        $res = array(
                'day' => $days,
                'day_cn' => $days.'天',
                'hour' => $hours,
                'hour_cn' => $hours.'小时',
                'minute' => $mins,
                'minute_cn' => $mins.'分钟',
                'second' => $secs,
                'second_cn' => $secs.'秒',
        );

        return $res;
    }

$begin_time = '1579313497';
echo date('Y-m-d H:i:s',$begin_time).'<br>';
$end_time = '1579404927';
echo date('Y-m-d H:i:s',$end_time).'<br>';
$res = Util::timediff($begin_time,$end_time);
print_r($res);

//输出:
2020-01-18 10:11:37
2020-01-19 11:35:27
Array ( [day] => 1 [day_cn] => 1天 [hour] => 1 [hour_cn] => 1小时 [minute] => 23 [minute_cn] => 23分钟 [second] => 50 [second_cn] => 50秒 )

获取当前时间的时间戳-到毫秒

    /**
     * 获取时间戳到毫秒
     * @return bool|string
     */
    function getMillisecond(){
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectimes = substr($msectime,0,13);
    }
    echo getMillisecond(); //输出:1579404784307

根据时间搓获取星期几:

    /**
     * 返回星期几
     * @param unknown $number_wk
     * @return Ambigous <string>
     */
    function getWeekDay($timestamp){
        $number_wk = date("w",$timestamp);
        $weekArr = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
        return $weekArr[$number_wk];
    }
echo getWeekDay(time()); //输出:星期日
posted @ 2020-01-19 11:45  柴高八斗  阅读(405)  评论(0编辑  收藏  举报