4

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. "'

3 Answers 3

5

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') . "'
Sign up to request clarification or add additional context in comments.

3 Comments

The timestamp_column contains random times, is there a way to ignore the time and just compare the date?
@David: that is why I added BETWEEN. Currently query retrieves everything between today noon and tomorrow noon
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.
0

Simply

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

MySQL date() function

3 Comments

I assume that this link will convince me that my solution is slower, am I right? ^^
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.
0

You could always convert the timestamp to a formatted date.

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

Produces: 2013 22nd January

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.