How many hours has it been since X

  1. What do you want to achieve?
    Get the time in hours that it has been since (example: 2021-09-27 16:38:32)
    I need it to result in for example “8 hours ago”

  2. What is the issue?
    I failed and don’t know how to create a reliable way of doing this.

local uploadDate = "2021-09-27 16:38:32"
local hoursPast = "???"

convert the upload date to seconds by dividing the minutes by 60, hours by 60^2, days by (60^2)/24, months by days/30, years by months/12. recursively do this, starting from years to months and so forth till you get one big seconds number

I personally find working with Unix timestamps far easier. If you don’t mind making it a bit less easy to read, you can use a unix timestamp converter to make the “uploadDate” something like “1632775112”. This way, you can simply subtract it with another timestamp to get the difference in seconds.

However, because of a lack of context and other code, I’m not sure if something like “uploadDate” is being retrieved by something like a web service that doesn’t come pre-formatted in a unix timestamp… so this might not be the ideal solution for you.

Here’s a great online generator and converter for unix timestamps, and you can get the current timestamp with os.time().

uploadDate comes from a PHP server.

I am searching for the PHP function

Well, I guess I worded that wrong. Where it comes from is usually not the deciding factor… I just sort of expected it to already return a Unix timestamp, since it’s used so often lol

What I really mean is, if you can work in just Unix timestamps in your code, it’s far easier than using something like a custom function to split up time and then having to do a ton of calculations.

You could just subtract the seconds and do far less calculations to return how long ago something… happened, I guess.