文章来源:https://www.lelee.top/jishuboke/phphuoquhaomiaojibieshijianchuojizhuanhuanriqi.html
public function index()
{
$msectime = $this->getMsecTime();
$mescdate = $this->getMsecToMescdate($msectime);
$msectime2 = $this->getDateToMesc($mescdate);
echo $msectime."<br />";
echo $mescdate."<br />";
echo $msectime2."<br />";
}
/**
* 获取毫秒级别的时间戳
*/
public function getMsecTime()
{
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}
/**
* 毫秒转日期
*/
public function getMsecToMescdate($msectime)
{
$msectime = $msectime * 0.001;
if(strstr($msectime,'.')){
sprintf("%01.3f",$msectime);
list($usec, $sec) = explode(".",$msectime);
$sec = str_pad($sec,3,"0",STR_PAD_RIGHT);
}else{
$usec = $msectime;
$sec = "000";
}
$date = date("Y-m-d H:i:s.x",$usec);
return $mescdate = str_replace('x', $sec, $date);
}
/**
* 日期转毫秒
*/
public function getDateToMesc($mescdate)
{
list($usec, $sec) = explode(".", $mescdate);
$date = strtotime($usec);
$return_data = str_pad($date.$sec,13,"0",STR_PAD_RIGHT);
return $msectime = $return_data;
}
效果如下:

PHP毫秒时间戳转换
该博客介绍了如何在PHP中进行毫秒时间戳与日期之间的转换。提供了三个方法:获取毫秒级别的时间戳、毫秒转日期以及日期转毫秒。通过示例代码展示了具体的转换过程。
750

被折叠的 条评论
为什么被折叠?



