- PHP 时间类的功能函数(收集中)
- 获取毫秒级别的时间戳
- 时间戳转日期格式(精确到毫秒)
- 时间日期转时间戳格式(精确到毫秒)
- 周日期(取到每周从几号开始到几号结束)
// 获取毫秒级别的时间戳
function getMillisecond() {
list($t1, $t2) = explode(' ', microtime());
return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
}
// 时间戳转日期格式(精确到毫秒,x代表毫秒)
function get_microtime_format($time)
{
if(strstr($time,'.')){
sprintf("%01.3f",$time); //小数点。不足三位补0
list($usec, $sec) = explode(".",$time);
$sec = str_pad($sec,3,"0",STR_PAD_RIGHT); //不足3位。右边补0
}else{
$usec = $time;
$sec = "000";
}
$date = date("Y-m-d H:i:s.x",$usec);
return str_replace('x', $sec, $date);
}
// 时间日期转时间戳格式(精确到毫秒)
function get_data_format($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;
}
/**
* 返回本周的开始日期到结束日期
* @return array
*/
function weekDates()
{
//当前日期
$default_date = date("Y-m-d");
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$first = 1;
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$w = date('w', strtotime($default_date));
//获取本周开始日期,如果$w是0,则表示周日,减去 6 天
$week_start = date('Y-m-d 00:00:00', strtotime("$default_date -" . ($w ? $w - $first : 6) . ' days'));
//本周结束日期
$week_end = date('Y-m-d 23:59:59', strtotime("$week_start +6 days"));
return array($week_start, $week_end);
}
转载请注明:隨習筆記 » PHP 时间类的功能函数(收集中)