php strtotime 以秒和分钟为单位

标签 php date strtotime date-math

我使用 ths 方法来查找两个时间戳之间的差异并获取这两个时间之间的秒数,然后像计数器一样用 jquery 刷新信息。

$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = intval(date('s', $diff));
echo $time;

当差值超过 60 秒时,$time 回到 0,就像重置一样。

例如,我想显示 1 min XX s

最佳答案

date()s 标志永远不会返回大于 59 的值,因为它只表示给定时间的当前秒数,永远不会超过在进入新的一分钟之前超过 59。

如果您想要总秒数,您实际上可以删除第二行代码,因为两个 Unix 时间戳之间的差异始终以秒为单位:

$time = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
echo $time;

如果您想将其显示为分钟和秒,您可以使用 DateTime(),它为此提供了更好的工具:

$now = new DateTime();
$then = new DateTime('2014-06-25 14:50:03');
$diff = $now->diff($then);
echo $diff->format('%i minutes %s seconds');

关于php strtotime 以秒和分钟为单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24409242/

相关文章:

javascript - 使用带有ajax的post请求将html表单数据发送到php到https ://foo. com

php - 带地理定位的 Swift 自动推送通知

php - 套接字 - 超时

php - 使用 php 验证多个 mysql 行之间的 true

javascript - '时光倒流' xkcd

php - php时区设置的建议

c# - 更改 ListView 中的日期格式#

date - 如何在 Go 语言中生成随机日期?

php - strtotime 在使用硬编码字符串形式的值时不返回值

PHP 添加周五无法正常工作的工作日