<?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;
}
}
}
【常用类库】时间帮助类
最新推荐文章于 2022-11-16 22:47:28 发布