Unix timestamp to days, hours, minutes

前端 未结 4 833
南方客
南方客 2021-01-07 06:22

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.

I would like

4条回答
  •  离开以前
    2021-01-07 06:48

    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.');
    

    (current_timestamp can be replaced with unix_timestamp(now()) if you want it calculated in-place)

    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.

    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.

提交回复
热议问题