skip to Main Content

How to compare now to a timestamp in Mysql, when valid_date is greater than now 1, else 0?

I only have data of valid_date, I need data of is_valid, thanks so much for any advice.

  valid_date   is_valid
  1675739460     0
  1680837060     1
  1678190564     1
  1686365124     1 

2

Answers


  1. You should be able to SELECT a boolean result by comparing the UNIX_TIMESTAMP() with valid_date.

    SELECT valid_date, UNIX_TIMESTAMP() < valid_date AS is_valid FROM table
    
    Login or Signup to reply.
  2. To compare a timestamp to the current time in MySQL, you can use the NOW() function, which returns the current date and time.
    To create a condition where valid_date is greater than NOW() – INTERVAL 1 DAY, you can use the following query:

    SELECT IF(valid_date > NOW() - INTERVAL 1 DAY, 1, 0) AS is_valid FROM table;
    

    You can adjust the time interval as needed by changing the value passed to INTERVAL.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search