Mysql table with datetime field, how to know how many days have passed?

Asked

Viewed 84 times

1

Hello, I have a table where there are the following fields.

prod_id [int 11]
prod_nome [varchar 100]
prod_desc [textarea]
prod_valor [double 10,2]
prod_data_cadastro [datetime]

I need to create a SELECT which shows the time spent until the current date using the column record prod_data_cadastro [datetime].

I tried so:

SELECT *, DATEDIFF(NOW(), `prod_data_cadastro`) as `data_corrida` FROM `produto` ORDER BY `data_corrida` DESC;

But returns me only the amount of running days, hours and minutes no, it is possible to bring date and time too?

1 answer

3


I can give you two different ways to do this. Remembering that the comparison is made with the server time.

1st

SELECT CONCAT(
FLOOR(HOUR(TIMEDIFF(prod_data_cadastro,NOW())) / 24), ' dias ',
MOD(HOUR(TIMEDIFF(prod_data_cadastro, NOW())), 24), ' horas ',
MINUTE(TIMEDIFF(prod_data_cadastro,NOW())), ' minutos') as `data_corrida`
FROM produto;

2nd

SELECT CONCAT(
TIMESTAMPDIFF(day,prod_data_cadastro,NOW()) , ' dias ',
MOD( TIMESTAMPDIFF(hour,prod_data_cadastro,NOW()), 24), ' horas ',
MOD( TIMESTAMPDIFF(minute,prod_data_cadastro,NOW()), 60), ' minutos '
) as `data_corrida`
FROM produto;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.