Tuntex Tuntex - 3 years ago 162
PHP Question

compare dates in mysql where the day, month and year are stored separately

I would like to compare several entries, for which I have the day, month and year saved in separate variables. When I try the following, I do not get the right answer:

$rescount=mysql_query('SELECT COUNT(*) FROM `sdcheckings` WHERE `dchecked` = 1 AND `email` ="'.$_SESSION['email'].'" AND `cday` >'.date('j').' AND `cmonth` >='.date('n').' AND `cyear` >='.date('Y'));

The reason is that this would ignore and entry that is in the following month, but that has a smaller day number.
What would be the easiest way to fix this?

Answer Source

I think that you can use CONCAT_WS to create date in query

DATE(CONCAT_WS('-',cyear,cmonth,cdate)) AS date

Then you got date in yyyy-mm-dd format to compare

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download