【问题标题】:Calculate passed time with PHP用 PHP 计算经过的时间
【发布时间】:2013-02-25 09:17:28
【问题描述】:

我得到了这样的日期之间的时差:

$time1 = "2013-02-25 12:00:00";
$time2 = "2013-01-01 12:00:00";
$tdiff = strtotime($time1) - strtotime($time2);

我想从$tdiff 中提取日期、小时和分钟。输出应该是:35 days 6 hours 14 minutes

我真的搜索并尝试自己做一些事情。但我无法获得真正的价值。

---- 编辑 ---- 我可以找到日期差异。我想从计算的时间中提取天、小时、分钟...

---- 编辑 2 ---- 这是我完整的mysql代码

select (
    select avg(UNIX_TIMESTAMP(tarih)) from table1 
    where action in (6) and who = '".$user."' and dates between '".$date1."' and '".$date."'
    ) - ( 
    select avg(UNIX_TIMESTAMP(tarih_saat)) from table2 
    where action in (6) and active = 1 and dates between '".$date1."' and '".$date2."
)

这个查询返回给我时间的真实价值。这个查询对我来说工作正常。结果就像:215922。结果类型为 UNIX_TIMESTAPM。所以我想知道这个时间戳有多少天、多少小时和多少分钟。

【问题讨论】:

标签: php time datediff


【解决方案1】:

PHP 有一个 DateInterval 类,可以这样使用:

$date1 = new DateTime('2013-02-25 12:00:00');
$date2 = new DateTime('2013-01-01 12:00:00');

$diff = $date2->diff($date1); // Get DateInterval Object

echo $diff->format('%d Day and %h hours and %i minutes');

【讨论】:

【解决方案2】:

您可以使用日期对象来获得更准确的信息。
默认的 var 类型不给出时差。
大多数情况下,当像您那样分配时,它们被视为字符串类型

【讨论】:

  • 完全多余的答案
猜你喜欢
  • 2011-12-12
  • 1970-01-01
  • 2016-04-25
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
  • 2012-04-09
  • 1970-01-01
相关资源
最近更新 更多