views:

144

answers:

6

I have a list of unix timestamps in a database, and I wanting to select the ones that are from today.

i.e If today is Tueday, I want to get all the timestamps that were made today? Is it possible? Is there such a things as strtotime("Today")?

Any help would be great

+2  A: 

you can use mktime() to generate the timestamp for the start of the day and then find the database entries with a timestamp greater than that.

Ty W
Also, you can use `getdate()` as your information source for `mktime()`, setting the values to 0 for the time. (I make the sole assumption that you work in UTC values and only do time zone conversion for display purposes.)
Michael Trausch
A: 

Check if DAY(NOW()) and MONTH(NOW()) and YEAR(NOW()) is equal to appropriate value of DAY(timestamp) and MONTH(timestamp) and YEAR(timestamp).

select timestamp from table where DAY(NOW()) = DAY(timestamp) AND MONTH(NOW()) = MONTH(timestamp) AND YEAR(NOW()) = YEAR(timestamp)
TBH
That won't work - the original post states that the date is stored as a unix timestamp.
adam
You're right, my bad.
TBH
+1  A: 

You can convert the unix timestamps to sql dates in the SQL using FROM_UNIXTIME(), then compare those to NOW()

SELECT * FROM `tablename` WHERE DATE(FROM_UNIXTIME(`dateFld`)) = DATE(NOW());
adam
FROM_UNIXTIME() is a MySQL only function. sea_1987 doesn't say what DBMS he's got.
symcbean
Fair enough, you know what they say about assumptions!
adam
@symcbean is absolutely right, but when the language is PHP, mySQL is a good guess statistically.
Pekka
A: 

If you're using mysql:

SELECT * FROM table WHERE DATE(NOW()) = DATE(FROM_UNIXTIME(timestampcol))
Chris AtLee
Same as my answer!
adam
A: 

FROM_UNIXTIME(somefield) can be compared to CURDATE() assuming you're using MySQL

SELECT * FROM mytable WHERE FROM_UNIXTIME(datefield,'%Y-%m-%d') = CURDATE();

ETA: Okay, I was assailed by doubt when this answer was marked down. So I went and did a couple of tests. Given MySQL it definitely works. So why the downmod?

Consider this test which outputs 2 identical fields for every row in a table:

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(CURDATE()),'%Y-%m-%d')  a , CURDATE() b
  FROM tablewithsomerows 
  WHERE FROM_UNIXTIME(UNIX_TIMESTAMP(CURDATE()),'%Y-%m-%d') = CURDATE();
dnagirl
+1  A: 
$start = strtotime(date('Y-m-d 00:00:00')); // Current date, at midnight
$end = strtotime(date('Y-m-d 23:59:59')); // Current date, at 11:59:59 PM

then, you can just select where the timestamp is between the above 2 timestamps:

"SELECT FROM `foo` WHERE `timestamp` BETWEEN '{$start}' and '{$end}'"
Harold1983-