【问题标题】:How to find out the days and minutes between 2 timestamps如何找出两个时间戳之间的天数和分钟数
【发布时间】:2012-03-18 05:59:16
【问题描述】:

我试图找出两个时间戳之间的年、月、日和分钟。

我使用了以下代码,但它不起作用:

$diff = time()-$res['asked_on'];
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

我得到的输出是:

0
0
0

$res['asked_on'] = 1331980897 (也是从time()函数得到的时间戳)

【问题讨论】:

  • 什么不起作用? $res['asked_on'] 也是时间戳吗?
  • timestamp - 对我说 db,大多数 db 都有这方面的功能,但你没有说你在使用什么。
  • 如果差异小于 24 小时,您将获得 0 天,因为 0.9999 天将降至 0。

标签: php date time timestamp


【解决方案1】:

如果$res['asked_on']是日期格式的字符串,则应使用strtotime将其转为时间戳。

如果你的 php >= 5.3,你可以看看 php 提供给你的方法:DateTime::diff

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 2019-09-10
    • 2020-11-25
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2017-05-03
    相关资源
    最近更新 更多