如何将秒数成分钟,php? [英] How to count seconds into minutes, php?

查看:21
本文介绍了如何将秒数成分钟,php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
输出以秒为单位.在php中转换为hh:mm:ss格式
如何显示秒前/分钟前/带有 unix 时间戳?

我正在处理这个用户在线脚本.到此为止如下...

I have this users online script I am working on. So far it is as follows...

while ($online_user = $q -> fetch(PDO::FETCH_ASSOC)) {

$last_active = time() - $online_user['online'];
echo '<p>' . $online_user['username'] . ' was last active ' . $last_active . ' seconds
    ago.</p>';

}

$online_user['online'] 是用户最后一次在网站上活跃的时间().因此,当我从当前 time() 减去它时,它会给我它们上次活动的秒数.

$online_user['online'] is the last time() the user was active on the website. So when I minus it from the current time() it gives me the amount of seconds ago that they were last active.

事情是这样的:

Some username was last active 567 seconds ago.

我想知道如何让它回声,以便将其转换为分钟和秒,使其看起来像这样;

I would like to know how I can make it echo so it would instead convert it to minutes and seconds so it would look something like this;

Some username was last active 9 minutes and 27 seconds ago.

提前致谢,如果您知道我可以在哪里学习,请发布链接.:)

Thanks in advance if you know of anywhere I could learn this please post a link. :)

推荐答案

你可以除以 60 得到分钟数.除法余数是不是一分钟的秒数.

You can divide by 60 to get the number of minutes. The division remainder is the number of seconds not in a minute.

$minutes = floor($last_active / 60);
$seconds = $last_active % 60;
echo 'Some username was last active ';

if ($minutes > 0) {
    echo "$minutes minutes ";
}

if ($seconds > 0) {
    if ($minutes > 0) {
        echo "and ";
    }
    echo "$seconds seconds ";
}
echo "ago.";

这会将其分解为分钟和秒,并且如果任何一个为零(即没有0 分钟和 30 秒前"),则不会输出分钟或秒.请注意,如果您想添加小时(X 小时), Y 分钟, Z 秒),你可以采用同样的方法,再次除以 60(每小时 60 分钟,每分钟 60 秒).要得到天",你可以除以 24(每天 24 小时).

This will break it down into minutes and seconds, and won't output the minutes or seconds if either is zero (ie. no "0 minutes and 30 seconds ago". Note if you ever want to add hours (X hours, Y minutes, Z seconds), you can just take the same approach, dividing again by 60 (60 minutes per hour, 60 seconds per minute). To get 'days', you would divide by 24 (24 hours per day).

这篇关于如何将秒数成分钟,php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆