【毫秒级】时间

<?PHP

/*

 * Created on 2018-03-12  毫秒级时间

 * Programmer : andy

 * Develop a project PHP - MySQL - Apache

 */

namespace Common\Model;

 

class MsecTimeModel

{

    /**

     * desc 返回当前的毫秒时间戳

     */

    function getMsectime() {

        list($msec, $sec) = explode(' ', microtime());

        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000000);

        return $msectime;

 

    }

    /**

     *desc 时间戳 转   日期格式 : 精确到毫秒,x代表毫秒

     */

    function getMicrotimeFormat($time)

    {

        $time=$time*0.000001;

        if(strstr($time,'.')){

            sprintf("%01.6f",$time); //小数点。不足6位补0

            list($usec, $sec) = explode(".",$time);

            $sec = str_pad($sec,6,"0",STR_PAD_RIGHT); //不足6位。右边补0

        }else{

            $usec = $time;

            $sec = "000000";

        }

        $date = date("Y-m-d H:i:s.x",$usec);

        return str_replace('x', $sec, $date);

    }

 

    /**

     * @desc 时间日期转时间戳格式,精确到毫秒

     */

    function getDataFormat($time)

    {

        list($usec, $sec) = explode(".", $time);

        $date = strtotime($usec);

        $return_data = str_pad($date.$sec,13,"0",STR_PAD_RIGHT); //不足13位。右边补0

        return $return_data;

    }

    /**

     *desc 获取当前时间 精确到秒

     */

    function getCurrenMsecTime()

    {

        return $this->getMicrotimeFormat($this->getMsectime());

    }

    /**

     *desc 时间毫秒级改为到秒

     */

    function getTimeByMsecTime($MsecTime)

    {

        $time=strtotime($MsecTime);

        if(empty($MsecTime) or $MsecTime==null or $time==0)

            return '0000-00-00 00:00:00';

        return date('Y-m-d H:i:s',$time);

    }

}

?>

 

转载于:https://www.cnblogs.com/fyandy/p/8688154.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值