【问题标题】:How to avoid adding timezone when subtract tow time in php在php中减去两次时如何避免添加时区
【发布时间】:2015-01-17 21:30:50
【问题描述】:

我的 php 时区是 Asia/Dhaka (UTC+6)

让我有两次时间

$a = 11:00:00;
$b = 09:00:00;
$sub = $a-$b; //it should return 02:00:00. But it adding the timezone and returns 08:00:00 (02:00:00 + 06:00:00)

如何避免从此输出中添加时区时间?

【问题讨论】:

  • Hi Nur,在运行此之前,您是否尝试过将默认时区设置为 Asia/Dhaka。
  • @Rohit Batra 默认时区在我的应用程序中设置为“亚洲/达卡”

标签: php datetime time timezone


【解决方案1】:

尝试使用 php 日期时间

$a = '11:00:00';
$b = '09:00:00';

$start_date = new DateTime($a);
$since_start = $start_date->diff(new DateTime($b));
echo "{$since_start->h}:{$since_start->i}:{$since_start->s}";

【讨论】:

  • 如果你用DateInterval::format()格式化输出会更好,比如echo $since_start->format('%H:%I:%S');
猜你喜欢
  • 1970-01-01
  • 2014-03-18
  • 2018-02-20
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
相关资源
最近更新 更多