I
I
ILoveYAnny2016-04-19 23:24:58
PostgreSQL
ILoveYAnny, 2016-04-19 23:24:58

How to get the difference in seconds (minutes) between the current date and an entry in a PostgreSQL DB?

With a certain action, I log the Timetamp to the database, after the Nth amount of time, I perform another action, and if 2 minutes have passed (And 140 seconds is best), I get the result and add another entry to the database, that is I'm updating the last log and I'm already reconciling it.
Implemented several solutions, but all of them do not suit me and have drawbacks. In short, during the second action I get I get the date of the first action from the database, through I bring the result to the desired format through DateTime And I get the current date through new DateTime
SELECT ::to_char(time_send, 'HH24:MI:SS')
Then I tried to compare with the built-in diff method, as I just didn’t try minutes and seconds .. The problem is that it resets the seconds and minutes counter when it reaches 60, that is, I can’t know exactly how many seconds or minutes have passed, because to. the difference will always be within 60.
I read that there is a parameter that gives an absolute value, but it didn’t display the result for me. Of course, you can get the time through time () or date () to do a bunch of conversions and subtract, but I want an adequate elegant way to find. Ideally, in 1 line without PHP, a query in PostGRE, where there is a check with a cell containing a date. If 140 seconds have passed, then return true, if not, then fall, or at least return the number of units of elapsed time ..
$diff = $last->diff($now, true);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
miholeus, 2016-04-19
@ILoveYAnny

select extract(epoch from (now() - time_send::timestamp))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question