ikbal
ikbal

Reputation: 11

Compare date with where clause

$date=date("d/m/Y");
$d =  "select * from admission  WHERE batcht > $date";
$e = mysql_query($d);
while($f=mysql_fetch_array($e)){
echo $f[2];
}

I want to fetch the names where current date exceeds due date.it does'nt work. please help me

Upvotes: 1

Views: 72

Answers (5)

Professor Abronsius
Professor Abronsius

Reputation: 33813

cast the date value in the varchar column batcht to a date before comparing with another date.

   select * from `admission`  WHERE date( `batcht` ) > str_to_date( '$date', '%Y/%m/%d' )

Thanks for the headsup @Sougata regarding date ~ didn't notice the invalid date format

Upvotes: 1

For mysql you can use NOW() for current date time or alternatively CURDATE() for current date. So you query should be like this:

select * from admission  WHERE batcht > CURDATE()

And ditch $date variable since you don't need it now.

Upvotes: 0

vikas pal
vikas pal

Reputation: 26

Comparision can only be performed in dates if and only if the datatype is been set to DATE or TIMESTAMP otherwise it will consider the value in db as a string and hence the results will be based on the length of string and not the data.

Hence I suggest you to please change the datatype of batcht column in the database

Upvotes: 0

Monty Khanna
Monty Khanna

Reputation: 1120

Database : postgresql

If you want to check with date then not necessary to use php date function.

you can change in your query.

select * from admission  WHERE batcht > CURRENT_DATE;

Upvotes: 0

hherger
hherger

Reputation: 1680

The date in mySQL has this format: 'YYYY-MM-DD hh:mm:ss'.
So set $date liken this:

$date = date('Y-m-d H:i:s);
$d = "select * from admission WHERE batcht > '$date'";

...or, alternatively:

$date = date('Y-m-d') . ' 00:00:00';
$d = "select * from admission WHERE batcht > '$date'";

Upvotes: 0

Related Questions