0

in my database i use a column named startdate and in the column there are rows with timestamps, looking like: 1410178260

Normally, when i use a datetime field and i want to select all the items with the date of today, i run this query:

$sql = "SELECT id FROM agenda2 WHERE DATE(startdate) >= CURRENT_DATE()";

But now, using the timestamps, i don't know how to make a query that selects all the items inserted today. Can someone help me with that?

2 Answers 2

1

You need to convert to date using the function from_unixtime()

mysql> select FROM_UNIXTIME('1410178260');
+-----------------------------+
| FROM_UNIXTIME('1410178260') |
+-----------------------------+
| 2014-09-08 17:41:00         |
+-----------------------------+

So you may do as

SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()
Sign up to request clarification or add additional context in comments.

2 Comments

A Sargable query will be SELECT id FROM agenda2 WHERE startdate >= UNIX_TIMESTAMP(CURRENT_DATE())
Yes Sargable query will make it better in terms of performance when the index is involved :)
0

If you're using unix timestamps then you've got to use

$sql = "SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()";

Consider that both versions can't make use of an index.

see FROM_UNIXTIME

Comments

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.