【问题标题】:add some hours to time with microseconds php用微秒 php 添加几个小时
【发布时间】:2015-03-02 07:43:27
【问题描述】:

您好,我需要在我格式化的时间中添加 8 小时,如下所示

            $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")   // 2015-03-02 07:37:18.715271

我使用这种格式是因为我还需要显示微秒。现在我需要在此增加 8 小时。您能建议我如何做到这一点。

谢谢

【问题讨论】:

  • 请尝试我的解决方案
  • 感谢它现在工作正常。 :)

标签: php time format add hour


【解决方案1】:
$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
print $date = $d->format("Y-m-d H:i:s.u");
print $new_time = date("Y-m-d H:i:s".$micro, strtotime($date.'+8 hours'));

【讨论】:

  • tghanks 的答案,但有一个问题 8 小时后它使 2015-03-02 15:57:42.00000 时间像这样我的意思是它使 microseonds 全部为 0。请检查
  • 抱歉,现在试试。我的错。我解决了这个问题
  • 微秒(在 PHP 5.2.2 中添加)。请注意,date() 将始终生成 000000,因为它需要一个整数参数,而 DateTime::format() 确实支持微秒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-07
  • 2013-01-12
相关资源
最近更新 更多