Hi all! I have a field in Database type longtext that stores a expiry date (dd-mm-yyyy) of a promotion. The code compares the expiry date with current date and perform an update in database. --------------------------------------------------------- $data_actual=date("d-m-Y"); print "<table width=\"100%\"><tr>"; $contador=0; while ($campos = mysql_fetch_array($result)) { $validade=$campos['validade_prom']; $id=$campos['id']; if ((date("d-m-Y",strtotime($data_actual))) > (date("d-m-Y",strtotime($validade)))){ mysql_query("UPDATE produto SET promocao = 0, validade_prom = '' WHERE id = $id "); mysql_query("update sub_produto set qtd_desc = '', preco_desc = '', desconto = '', promocao = 0 where produto = $id "); } else{ --------------------------------------------------------- The problem is that if I place a older date (like 02-02-2007) the system doesnt perform the update. But if the older date is more recent (27-03-2007) it does the update! Cant understand why this is happen! Anyone? António
Try this instead: if (strtotime($data_actual) > strtotime($validade)){ PHP: You can use timestamps to compare. No need to reformat it. With the other method, it should be in this format: yyyy-mm-dd, so you can compare it.
take a look at this ordered string: 01-01-2007 01-02-2007 01-03-2007 02-01-2007 02-02-2007 you can see that 01-03-2007 is smaller than 02-01-2007 which is incorrect
But shouldn't 02-01-2007 be smaller if this is dd-mm-yyyy? Either way, did you try my other suggestion?