【常用类库】时间帮助类

<?php
/**
 * 时间帮助类
 * Created by PhpStorm.
 * User: yaoxiaofeng
 * Date: 17/10/24
 * Time: 下午10:51
 */
namespace App\Lib;

class DateHelper
{
    const YYYYMMDDHHIISSsss = "YYYYMMDDHHIISSsss";
    const YYYY_MM_DD_HH_II_SS_sss = "YYYY_MM_DD_HH_II_SS_sss";

    public static function thisWeekFirstDay($timestamp = false)
    {
        $week = date('W',time());
        $time = date('Y-m-d',strtotime(date('Y').'-'.'W'.$week.'-1'));
        return $timestamp ? strtotime($time):$time;
    }

    public static function thisMonthFirstDay($timestamp = false)
    {
        $time = date('Y-m-01');
        return $timestamp ? strtotime($time):$time;
    }

    public static function thisYearFirstDay($timestamp = false)
    {
        $time = date('Y-01-01');
        return $timestamp ? strtotime($time):$time;
    }

    /**
     * microsecond 微秒     millisecond 毫秒
     *返回时间戳的毫秒数部分
     */
    public static function getMillisecond()
    {
        list($usec, $sec) = explode(" ", microtime());
        return $sec . '.' . round($usec*1000);
    }

    /**
     * 精确到毫秒级  eg : 2017-12-01 17:37:47.354
     * @return string
     */
    public static function getTimeMilliSec()
    {
        list($usec, $sec) = explode(" ", microtime());
        return date('Y-m-d H:i:s', $sec) . '.' . round($usec * 1000);
    }

    /**
     * 时间格式化
     * @param $format
     * @return string
     */
    public static function getTimeFormat($format)
    {
        if($format == self::YYYYMMDDHHIISSsss) { // eg:20171012213922
            //获取毫秒数
            list($usec, $sec) = explode(" ", microtime());
            $millisecond = round($usec*1000);
            $millisecond = str_pad($millisecond,3,'0',STR_PAD_RIGHT);
            return date('YmdHis').$millisecond;
        }
        if($format == self::YYYY_MM_DD_HH_II_SS_sss){ //eg : 2017-12-01 17:37:47.354
            list($usec, $sec) = explode(" ", microtime());
            return date('Y-m-d H:i:s', $sec) . '.' . round($usec * 1000);
        }
        return "";
    }

    /**
     * 获取时间差集
     * @param string $type, day|当天,month|当月
     * @return int
     */
    public static function getDiffTimes($type = "day")
    {
        if($type == 'day'){
            return mktime(23,59,59,date('m'),date('d'),date('Y'))-time();
        }elseif ($type == 'month'){
            return mktime(23,59,59,date('n'),date('t'),date('Y'))-time();
        }else{
            return 86400;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值