从毫秒获取日期格式 m-d-Y H:i:s.u [英] Getting date format m-d-Y H:i:s.u from milliseconds

查看:40
本文介绍了从毫秒获取日期格式 m-d-Y H:i:s.u的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取格式化的日期,包括以毫秒为单位指定的 UNIX 时间戳中的微秒数.

唯一的问题是我不断收到 000000,例如

$milliseconds = 1375010774123;$d = date("m-d-Y H:i:s.u", $milliseconds/1000);打印 $d;

<块引用>

07-28-2013 11:26:14.000000

解决方案

php.net 说:

<块引用>

微秒(在 PHP 5.2.2 中添加).请注意,date() 将始终生成 000000,因为它接受一个整数参数,而 DateTime::format() 确实支持微秒,如果 DateTime 是用微秒创建的.

所以使用起来很简单:

$micro_date = microtime();$date_array =explode(" ",$micro_date);$date = date("Y-m-d H:i:s",$date_array[1]);echo "日期: $date:" .$date_array[0]."
";

推荐并使用来自引用的 dateTime() 类:

$t = microtime(true);$micro = sprintf("%06d",($t - floor($t)) * 1000000);$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );打印 $d->format("Y-m-d H:i:s.u");//注意u"上的点

注意 u 是微秒(1 秒 = 1000000 µs).

来自 php.net 的另一个例子:

$d2=new DateTime("2012-07-08 11:14:15.889342");

php.net 上参考 dateTime()一个>

我已经回答了简短的问题并简化了作者.请参阅作者的更多信息:获取日期格式 mdYH:i:su 从毫秒开始

I am trying to get a formatted date, including the microseconds from a UNIX timestamp specified in milliseconds.

The only problem is I keep getting 000000, e.g.

$milliseconds = 1375010774123;
$d = date("m-d-Y H:i:s.u", $milliseconds/1000);
print $d;

07-28-2013 11:26:14.000000

解决方案

php.net says:

Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds.

So use as simple:

$micro_date = microtime();
$date_array = explode(" ",$micro_date);
$date = date("Y-m-d H:i:s",$date_array[1]);
echo "Date: $date:" . $date_array[0]."<br>";

Recommended and use dateTime() class from referenced:

$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );

print $d->format("Y-m-d H:i:s.u"); // note at point on "u"

Note u is microseconds (1 seconds = 1000000 µs).

Another example from php.net:

$d2=new DateTime("2012-07-08 11:14:15.889342");

Reference of dateTime() on php.net

I've answered on question as short and simplify to author. Please see for more information to author: getting date format m-d-Y H:i:s.u from milliseconds

这篇关于从毫秒获取日期格式 m-d-Y H:i:s.u的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆