8

When I send a form on my web, the insert query saves the current date on my DB using the function now().

Now, I'm trying to get this column in minute format to calculate other thinks that I need, but I don't know how to do that.

For example, I have this:

"2013-05-08 08:30:00"

And I want this (now 8.50):

"20" <- In minutes

Thanks

5
  • In the first line of the example the minutes are "30". In the second, "50". In the third "20". Can you explain a little better what you want? Commented May 8, 2013 at 6:55
  • 1
    Because between 8.30 and 8.50 pass 20 minutes Commented May 8, 2013 at 6:56
  • 1
    Ok, but what do you want if now is 9:40? A result of 70 or 10? Commented May 8, 2013 at 6:59
  • Yes, 70. I need the difference in minutes between now and the date in DB. Commented May 8, 2013 at 7:02
  • Now it is clear, thanks. I submitted an answer. Commented May 8, 2013 at 7:08

1 Answer 1

27

OK, let's suppose you have a table with a timestamp:

CREATE TABLE ex (t timestamp);
INSERT INTO ex VALUES ('2013-05-08 8:30'::timestamp);

And you want the difference in minutes between the column t and now(). You can get that using the extract function:

SELECT extract(epoch from (now() - ex.t)) / 60 FROM ex;

epoch is the number of seconds from the "epoch" for date and timestamp types, but is't just the number of seconds in the interval for interval types. By dividing it by 60 you get what you want (if you want an integer number of minutes just trunc it.)

Sign up to request clarification or add additional context in comments.

1 Comment

OMG. This community is amazing. YOU are amazing. Thanks so much, it works perfect!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.