如何使用PostgreSQL在几分钟内获得时间戳

时间:2022-11-11 04:16:27

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

当我在网上发送表单时,插入查询使用函数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):

我想要这个(现在是8.50):

"20" <- In minutes

“20”< - 分钟

Thanks

1 个解决方案

#1


7  

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:

你想要列t和now()之间的分钟差异。你可以使用提取功能得到它:

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

epoch是日期和时间戳类型的“纪元”的秒数,但不仅仅是间隔类型的间隔中的秒数。通过将它除以60得到你想要的东西(如果你想要整数分钟就可以截断它。)

#1


7  

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:

你想要列t和now()之间的分钟差异。你可以使用提取功能得到它:

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

epoch是日期和时间戳类型的“纪元”的秒数,但不仅仅是间隔类型的间隔中的秒数。通过将它除以60得到你想要的东西(如果你想要整数分钟就可以截断它。)