简体   繁体   中英

sql comparing dates wrongly

<?php         
     date_default_timezone_set('Asia/Kolkata');
     $currentDate =  date("d M Y h:i:s a");
     $sql=mysql_query("update products set past=0 where valid_date>'".$currentDate."'");
     $sql1=mysql_query("update products set past=1 where valid_date<'".$currentDate."'");
?>

The above is my code. if valid_date is 31 Jan 2017 12:25:07 pm the SQL query still takes this as future date(ie greater than today's date) but it takes 05 Feb 2017 12:25:07 pm as past. Precisely to say that the query is comparing February dates correctly and January dates wrongly.

what is wrong in my code?

Try this,

<?php         
     date_default_timezone_set('Asia/Kolkata');
     $sql=mysql_query("update products set past = CASE WHEN valid_date > NOW() THEN 0 WHEN valide_date < NOW() THEN 1 END where 1 = 1");
?>

This is documentation for NOW() mysql function.

And as your condition states, this code will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM