PHP代码总结:时间处理相关的辅助类

class TimeHelper
{
/**
* 得到当前时间的毫秒时间戳
* @return float 13位毫秒时间戳
*/
public static function getCurTimeMsec()
{
list($t1, $t2) = explode(' ', microtime());
return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
}

/**
* 获取当前时间17位的毫秒时间格式时间点(2017 07 29 21 44 43 129)
* @return integer
*/
public static function getCurTimeMsecFormat()
{
list($u_sec, $sec) = explode(' ', microtime());
return intval(date('YmdHis', intval($sec)) . str_pad(round(floatval($u_sec) * 1000), 3, 0, STR_PAD_LEFT));
}

/**
* 将20200629000000格式的日期转换成0000-00-00 00:00:00格式
*/
public static function dateNormalization($date)
{
if (strlen($date) == 14) {
return preg_replace('/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', "$1-$2-$3 $4:$5:$6", $date);
}
return $date;
}

/**
* 将17位的毫秒时间格式(2017 07 29 21 44 43 129)转换成毫秒时间戳格式(15XXX)
*/
public static function dateToMescNormalization($date)
{
if (strlen($date) == 17) {
$secDate = preg_replace('/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', "$1-$2-$3 $4:$5:$6", substr($date,0,14));
return strtotime($secDate).substr($date,14,3);
}
return $date;
}
}

0 个评论

要回复文章请先登录注册