Bhumi Shah
Bhumi Shah

Reputation: 9476

How to MySQL two dates sum and compare with current timestamp

I want to sum and compare dates with current time and logic I applied is below:

SELECT 
    (TIMESTAMP(field1) + TIMESTAMP(field2)) AS t,  
    TIMESTAMP(NOW()) AS tt 
FROM
    table1

But it is not working properly.comparison gives wrong results or empty.I thought it is timezone issue and I have set the common timezone for both PHP and MySQL.

Is there any other way to sum two dates and compare?

I want to compare like below in where

(TIMESTAMP(field1) + TIMESTAMP(field2)) <  TIMESTAMP( NOW( ) ) 

OR query:

SELECT * 
FROM table 
WHERE (TIMESTAMP(field1) + TIMESTAMP(field2))  as t , TIMESTAMP( NOW( ) )

Thanks

Upvotes: 1

Views: 97

Answers (1)

Rahul
Rahul

Reputation: 77896

Your current where condition doesn't make sense and is syntactically wrong as seen below

where (TIMESTAMP(field1) + TIMESTAMP(field2))  as t , TIMESTAMP( NOW( ) )

It should rather be

where TIMESTAMP(field1, field2) < TIMESTAMP(NOW())

You can pass both expression field1 and field2 to TIMESTAMP function. See Documentation

Upvotes: 2

Related Questions