John John - 3 years ago 234
MySQL Question

MySQL How to get timedifference in unix timestamp

I have variable that holds value 1506947452 and need to extract minutes from that date with formula: started_data - now_date but started_date is in unix timestamp format 10-digit int number...that i received in ajax form and need to put in mysql query i try with this:


but i get this from terminal:

| NULL |
1 row in set, 1 warning (0.00 sec)

So how i can compare two dates in unix_timestamp format and get minutes?
I need to use unix_timestamp for start_date so others date format is not solution.

Answer Source

UNIX_TIMESTAMP is seconds seconds since '1970-01-01 00:00:00' in UTC. If you have a variable holding another UNIX_TIMESTAMP, you can get the difference and turn seconds to minutes (and round/truncate the result if needed):

 SELECT ROUND((UNIX_TIMESTAMP()-1506947452) / 60, 0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download