tags:

views:

131

answers:

6

Need help with PHP/MySql. Need to select all the records from 'today'.

My table has a coloumn which holds a unixtime stamp and I want to just select from the table where the unixtime stamp = today.

Happy to do this on linux command line just need to basic MySql query?

+3  A: 

Since you tagged PHP I'll provide a PHP answer:

list ( $y, $m, $d ) = explode('.', date('Y.m.d')); 
$today_start = mktime(0, 0, 0, $m, $d, $y);
$today_end   = mktime(23, 59, 59, $m, $d, $y);
// do the query with this clause:
// ... WHERE unix_timestamp BETWEEN $today_start AND $today_end
kemp
you have to encapsulate your date string in `'` or `"` like `date('Y.m.d')`
RobertPitt
+2  A: 

SELECT * FROM table_name WHERE DATE(date) = DATE(NOW())

mnstrflkrn
+1  A: 

A suitable WHERE clause might be:

CAST( FROM_UNIXTIME( <your_field> ) AS DATE ) = CURDATE( )
  • FROM_UNIXTIME( ) - converts to MySQL DATETIME
  • CAST( AS DATE ) - gets just the date part
  • CURDATE( ) - gets the current date
martin clayton
+8  A: 

I'd go for the SQL version:

SELECT * FROM table WHERE DATE(timestamp_field) = CURDATE();

mbanzon
use DATE(FROM_UNIXTIME(timestamp_field)) if timestamp_field holds the no. of seconds since 1970
nos
+1 Though I would also use `CURDATE()` or `CURRENT_DATE` instead of `DATE(NOW())`
wimvds
There - I fixed it - now it use CURDATE() :-)
mbanzon
A: 
SELECT * FROM tbl WHERE date >= CURDATE()
Jarsäter
A: 
$q="select * from table where date >'".date("Y-m-d 00:00:00")."' and date <'".date("Y-m-d 23:59:59")."'";
Imran Naqvi