Forum Moderators: open
At first I thought I was creating an incorrect date with PHP however after I checked it seemed to be just fine. Here is the PHP code just in case I've got it wrong though...
$date_past = date('o-m-d H:i:s',time() - 1 * 30);
Here are some queries I've tried all of which return oddball results which are unquestionably wrong...
$query = "SELECT username, chat_date FROM accounts WHERE chat_date BETWEEN '".$date_past."' AND NOW()";$query = "SELECT username, chat_date FROM accounts WHERE chat_date <= '".$date_past."' AND chat_date >= NOW()";
$query = "SELECT username, chat_date FROM accounts WHERE chat_date > '".$date_past."'";
Either I'm doing something wrong or encountered a bug of some sort...I'm putting my money that I am doing something wrong here though. Thoughts please?
- John
I also find that mixing PHP generated timestamps and MySQL timestamps (comparing NOW() against time()) can cause trouble. Wherever possible, I try to generate all timestamps in the same way (pseudocode):
$query = "SELECT username, chat_date FROM accounts WHERE chat_date BETWEEN (NOW() - INTERVAL 30 SECONDS) AND NOW();";
Or
$query = "SELECT username, chat_date FROM accounts WHERE chat_date BETWEEN '".date('Y-m-d H:i:s', strtotime("-30 seconds"))."' AND '".date("Y-m-d H:i:s")."';";
Which I pick is entirely based on whether I'm feeling more sql-y that day or php-y. ;)