Strange error in date comparation

Discussion in 'PHP' started by afonseca, May 25, 2007.

  1. #1
    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
     
    afonseca, May 25, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    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.
     
    nico_swd, May 25, 2007 IP
  3. raredev

    raredev Peon

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    raredev, May 25, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    But shouldn't 02-01-2007 be smaller if this is dd-mm-yyyy? :)

    Either way, did you try my other suggestion?
     
    nico_swd, May 25, 2007 IP