Take the 2-minute tour ×

Any help is highly appreciated.

Say I have a MySQL database with a timestamp column with the value "1305590400", how can I compare that with a PHP variable of say "2011-05-17"? I want to completely ignore the time part and only compare the date.

At the moment I am trying to get it working with the following but it returns no results:

WHERE  FROM_UNIXTIME('timestamp_column','%Y-%m-%d') = '" . $date. "'
share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

You don't get results that probably $date has some time offset and is not equal to 00:00:00 in your timezone.

WHERE timestamp_column BETWEEN '" . strtotime($date) . "' AND '" . strtotime($date, '+1 day') . "'

or to be more precise:

    WHERE timestamp_column >= '" . strtotime($date) . "'
      AND timestamp_column < '" . strtotime($date, '+1 day') . "'
share|improve this answer
I never thought of trying that! I'll give it a go right now... –  David May 17 '11 at 0:27
+1 for +1 day :D –  alex May 17 '11 at 0:30
The timestamp_column contains random times, is there a way to ignore the time and just compare the date? –  David May 17 '11 at 0:33
@David: that is why I added BETWEEN. Currently query retrieves everything between today noon and tomorrow noon –  zerkms May 17 '11 at 0:34
The only problem I have is that I'm using this in a calendar script and I don't think it will return the right results, I will definitely try it out now though. –  David May 17 '11 at 0:36
add comment

You could always convert the timestamp to a formatted date.

SELECT FROM_UNIXTIME(1358871753,'%Y %D %M');

Produces: 2013 22nd January

share|improve this answer
add comment


SELECT [...] WHERE '$you_var' = date(`timestamp_column`);

MySQL date() function

share|improve this answer
How mysql uses indexes –  zerkms May 17 '11 at 0:33
I assume that this link will convince me that my solution is slower, am I right? ^^ –  Thomas Hupkens May 17 '11 at 0:36
yep. Your query will always cause fullscan. If you have a lot of rows and need to find fast - don't apply functions for the fields in your WHERE. –  zerkms May 17 '11 at 0:38
add comment

Your Answer


