Unix时间戳记以天,小时,分钟为单位 [英] Unix timestamp to days, hours, minutes
问题描述
因此,我的用户表中有一个名为last_active
的字段,该字段在用户每次重新加载页面时都会更新.
它存储在unix时间戳中.
So, I have a field in my users table named last_active
which updates every time a user reloads a page.
It's stored in unix timestamp.
我想这样输出:Last online: 4 d 18 h 19 m ago
那怎么办?可以用php的date()做到吗?
How would one do that? Can you do it with php's date()?
谢谢.
推荐答案
如果愿意,可以直接在MySQL中实现此目标
You could achieve this directly in MySQL if you like:
select date_format(from_unixtime(current_timestamp - last_timestamp),
'Last online: %e days, %k hours, %i minutes, %s seconds ago.');
(如果希望就地计算,可以用unix_timestamp(now())
替换current_timestamp
)
(current_timestamp
can be replaced with unix_timestamp(now())
if you want it calculated in-place)
DATE_FORMAT
允许您基于特定日期使用自定义字符串.如果您用两个时间戳之间的时差填充日期,它将按您的要求工作.
DATE_FORMAT
allows you to have a custom string based on a specific date. If you populate its date with the difference between two timestamps, it will work as you've asked.
上述解决方案仅在不到一个月的情况下有效;如果需要一年中的某几天,请使用%j.该功能的文档显示了更多.
The above solution will only work if it's under a month; if you want days of the year, use %j. The documentation for the function shows more.
这篇关于Unix时间戳记以天,小时,分钟为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!