[ create a new paste ] login | about

Link: http://codepad.org/BmvGt2qa    [ raw code | output | fork | 1 comment ]

ninwa - PHP, pasted on Oct 31:
<?php

// Take two dates in form of "dd/mm/yyyy" and compares them.
function compare_dates($d1, $d2)
{
    // simple validation
    if( strlen($d1) != 10 && strlen($d1) != 10)
        return "ERROR_MALFORMED_DATE";

    if( $d1[2] != "/" && $d1[5] != "/" &&
        $d2[2] != "/" && $d2[5] != "/" )
        return "ERROR_MALFORMED_DATE";

    $d1 = str_replace("/","",$d1);
    $d2 = str_replace("/","",$d2);

    $d1 = intval(substr($d1,0,2)) + (intval(substr($d1,2,2)) * 100) + (intval(substr($d1,4,4)) * 10000);
    $d2 = intval(substr($d2,0,2)) + (intval(substr($d2,2,2)) * 100) + (intval(substr($d2,4,4)) * 10000); 

    if($d1 > $d2){ return 1; }
    elseif( $d1 < $d2 ){ return -1; }
    else{ return 0; }
}

echo compare_dates("05/03/1988","05/03/2008") . "\n";
echo compare_dates("06/01/2992","03/29/2004") . "\n";

?>


Output:
1
2
-1
1


Create a new paste based on this one


Comments:
posted by ninwa on Oct 31
Accepts two dates in the form of "dd/mm/yy" and compares them.
reply