时间戳与时间相互转换(php .net精确到毫秒)

2019-12-26 13:29:58王振洲

本文给大家分享的时间戳与时间相互转换(php .net精确到毫秒) ,感兴趣的朋友一起学习吧

 

 
  1. /** 获取当前时间戳,精确到毫秒 */  function microtime_float() 
  2. {  list($usec, $sec) = explode(" ", microtime()); 
  3. return ((float)$usec + (float)$sec);  } 
  4. /** 格式化时间戳,精确到毫秒,x代表毫秒 */  function microtime_format($tag, $time) 
  5. {  list($usec, $sec) = explode(".", $time); 
  6. $date = date($tag,$usec);  return str_replace('x', $sec, $date); 

使用方法:

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

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

.net 时间戳互相转换(精确到毫秒)

这里记录一个时间戳的互相转换方法,网上都找了,基本都没有精确到毫秒,我的这个基本可以满足精确到毫秒的级别,代码如下:

 

 
  1. /// <summary>  /// Unix时间戳转换为DateTime 
  2. /// </summary>  private DateTime ConvertToDateTime(string timestamp) 
  3. {  System.DateTime time = System.DateTime.MinValue; 
  4. //精确到毫秒  //时间戳转成时间 
  5. DateTime start = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , ));  try 
  6. {  time = timestamp.Length == ? start.AddSeconds(long.Parse(timestamp)) : start.AddMilliseconds(long.Parse(timestamp));