且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP时间戳与时间相互转换(精确到毫秒)

更新时间:2022-08-15 23:15:15

原文:PHP时间戳与时间相互转换(精确到毫秒)

/** 获取当前时间戳,精确到毫秒 */

function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

 

/** 格式化时间戳,精确到毫秒,x代表毫秒 */

function microtime_format($tag, $time)
{
   list($usec, $sec) = explode(".", $time);
   $date = date($tag,$usec);
   return str_replace('x', $sec, $date);
}

 

使用方法:

1. 获取当前时间戳(精确到毫秒):microtime_float()

2. 时间戳转换时间:microtime_format('Y年m月d日 H时i分s秒 x毫秒', 1270626578.66000000)