1

I have table in mysql with three fileds

id
name
time_stamp

here time_stamp stores php time() value.

I want to get the fields that matches todays timestamp ,now i am fetching all rows and check time stamp condition in the loop if it matches will print the current row.

Is there anyway to do this in mysql query itself?

1000111
  • 13,169
  • 2
  • 28
  • 37
Blessan Kurien
  • 1,645
  • 9
  • 31
  • 63

4 Answers4

3

Since you insert unix timestamps (time()), you don't need to fetch all rows and make a comparison on the fetching. Just use FROM_UNIXTIME() in MySQL:

SELECT * FROM table_name WHERE FROM_UNIXTIME(`time_stamp`, '%Y-%m-%d') = CURDATE()

Fiddle

Kevin
  • 41,694
  • 12
  • 53
  • 70
1

You are storing UNIX_TIMESTAMP.

So, there's one way:

SELECT * FROM table_name WHERE FROM_UNIXTIME(time_stamp, '%Y-%m-%d') = CURDATE() @Ghost already posted this in his answer

But one potential problem with this approach is that you cannot make use of index (if any) on time_stamp column.

Here's the other way around:

SELECT * FROM 
table_name
WHERE time_stamp BETWEEN UNIX_TIMESTAMP(CURDATE()) AND (UNIX_TIMESTAMP(CURDATE() + INTERVAL 1 DAY) - 1)
1000111
  • 13,169
  • 2
  • 28
  • 37
0

You can use NOW() or Currentdate() function for that

0

You can do a between in database to get records from today for example, or you can convert date to the specific format that you need.

to human-readable you can use http://www.w3schools.com/sql/func_date_format.asp

if is the case use between with date_format and select only what you need.

i hope help you