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

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

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

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

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

///

/// Unix时间戳转换为DateTime

///

private DateTime ConvertToDateTime(string timestamp)

{

System.DateTime time = System.DateTime.MinValue;

//精确到毫秒

//时间戳转成时间

DateTime start = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , ));

try

{

time = timestamp.Length == ? start.AddSeconds(long.Parse(timestamp)) : start.AddMilliseconds(long.Parse(timestamp));

}

catch (Exception ex)

{

return start;//转换失败

}

return time;

}

///

/// DateTime转换为Unix时间戳

///

///

///

private string ConvertTimestamp(DateTime time)

{

double intResult = ;

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , ));

intResult = (time - startTime).TotalMilliseconds;

return Math.Round(intResult,).ToString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值