<?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";
?>