0

Im trying to make a query where time() is between date_start and date_end i tried:

date_start > CURRENT_TIMESTAMP AND date_end < CURRENT_TIMESTAMP

and

WHERE CURRENT_TIMESTAMP BETWEEN date_start AND date_end

Still it does not workout for me. Tried CURRENT_TIMESTAMP() too.

Mysql

id  item_id percentage  rabatcode   date_start  date_end
9   6       50          XXY-GGS-82  1323817200  1324422000  
3
  • Can you provide a sample of the date_start and date_end values as well as the schema definition? Commented Dec 14, 2011 at 20:58
  • Should work, unless your columns are not of a proper date type. Commented Dec 14, 2011 at 20:59
  • See updated @hafichuk . The columns are int 11 Commented Dec 14, 2011 at 21:02

3 Answers 3

3

Compare NOW() against the values via FROM_UNIXTIME():

WHERE NOW() BETWEEN FROM_UNIXTIME(date_start) AND FROM_UNIXTIME(date_end)

Ideally, if it is possible for you to modify your database structure, it is usually much better to store dates as MySQL's native DATETIME type, owing to the fact that MySQL's date processing functions work with the date types without conversion.

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

2 Comments

Thank you this worked!! can you tell me why my current_timestamp comparing with the other timestamps, wouldnt work?
@Karem CURRENT_TIMESTAMP is a synonym for NOW() so you could use that instead. But you would need to convert it to a UNIX timestamp from the native MySQL datetime format as UNIX_TIMESTAMP(CURRENT_TIMESTAMP). Above, I did the opposite, converting your two UNIX timestamps to MySQL format instead...
2

You mention in the title of your question that you want to check when the current time is between to UNIX timestamps, so you need to use the UNIX_TIMESTAMP() function:

WHERE UNIX_TIMESTAMP() BETWEEN date_start AND date_end

NOW() returns a string (from the manual: '2007-12-15 23:50:26') so it won't work for your purposes.

Comments

0

The problem is you cannot use CURRENT_TIMESTAMP twice. You should look into converting your dates to a different format and comparing them to NOW() (if they cannot be compared to NOW() already.

2 Comments

would you be able to provide a reference for not being able to use CURRENT_TIMESTAMP twice in a query?
AFAIK, CURRENT_TIMESTAMP is the same as NOW()

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.