Jump to content

[SOLVED] Mysql datetime/timestamp comparison with NOW() not working...


Niccaman

Recommended Posts

This has been driving me nuts for the last 2 hours. I have a timestamp column with a timestamp earlier than now.

It always returns the row to me, when it shouldnt. It should just come up with no results, but it keeps giving me the bloody row!!

 

My statement is:

$query = "SELECT * FROM `stuff` WHERE `daterenewal` > NOW() AND ...";
$query = "SELECT * FROM `stuff` WHERE `daterenewal` < NOW() AND ...";

 

Ive tried everything i can think of, including:

$query = "SELECT * FROM `stuff` WHERE `daterenewal` > CURDATE() AND ...";

$t = time();
$query = "SELECT * FROM `stuff` WHERE UNIX_TIMESTAMP(`daterenewal`) > $t AND ...";

 

Everything i tried failed. Ive even tried converting the column to datetime instead.

Link to comment
Share on other sites

erm.. if im not right when i say timestamp, i dont know what u mean.

its got like: "0000-00-00 00:00:00" as its value.

 

Ive tried putting the date within the range that timestamp is targeted too, with no luck.

Ive also tried changing the column type to datetime

Link to comment
Share on other sites

if it's a DATETIME column, then

$query = "SELECT * FROM `stuff` WHERE `daterenewal` > NOW() AND ...";

should return nothing and

$query = "SELECT * FROM `stuff` WHERE `daterenewal` < NOW() AND ...";

should return the row

 

can you post more code, maybe the problem is elsewhere...

Link to comment
Share on other sites

$query = "SELECT * FROM `stuff` WHERE `daterenewal` > NOW() AND ...";
$res=mysql_query($query);
  if (mysql_num_rows($res))
  {
       while ($row=mysql_fetch_object($res))
       {
            ... // This is always fulfilled and returning the row with date earlier than now.
       }
  }else
  {
    echo "No search results";
  }

Link to comment
Share on other sites

what is the output of:

 

<?php
$result = mysql_query("SHOW COLUMNS FROM `stuff`")
  or die('Could not run query: ' . mysql_error());
if (mysql_num_rows($result) > 0) {
  while ($row = mysql_fetch_assoc($result))
    print_r($row);
}
?>

Link to comment
Share on other sites

Everything looks good. The only thing I can think of is differences in Timezones. Is the output of this what you expect it to be?

 

<?php
$result = mysql_query("SELECT NOW()")
  or die('Could not run query: ' . mysql_error());
print_r(mysql_fetch_assoc($result));
?>

Link to comment
Share on other sites

One step ahead of you. I simply tested it without the extras after "..." so full sql query:

 

SELECT * FROM `stuff` WHERE `daterenewal` > NOW()

 

Conditions is still fulfilled. :(

 

// Cancel, i forgot to put the date a lesser value (from previous testing i changed it to above NOW())

and it now works.

 

My statement had some ORs after the "..." and i believe order of precedence meant it was like bracketing the first 2 conditions, whilst leaving the rest independent.

 

So,

"WHERE `x` = 'y' AND `x` = 'y' OR `x` = 'y'  ..."

became

"WHERE (`x` = 'y' AND `x` = 'y') OR `x` = 'y'  ..."

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.