Function Date Compare

มาดู Code ที่ใช้หาผลต่างของวันที่กันดีกว่า เอาไว้ใช้ประโยชน์ได้หลากหลายดีครับ
จากตัวอย่างข้างล่าง สามารถ Copy นำไปใช้ได้เลยครับ

<?php
// returns <0, 0, >0 if date a< date b,date a== date b,date a > date b respectively.
function compareDate ($i_sFirstDate, $i_sSecondDate)
{
//Break the Date strings into seperate components
$arrFirstDate = explode ("/", $i_sFirstDate);
$arrSecondDate = explode ("/", $i_sSecondDate);

$intFirstDay = $arrFirstDate[0];
$intFirstMonth = $arrFirstDate[1];
$intFirstYear = $arrFirstDate[2];

$intSecondDay = $arrSecondDate[0];
$intSecondMonth = $arrSecondDate[1];
$intSecondYear = $arrSecondDate[2];


// Calculate the diference of the two dates and return the number of days.


$intDate1Jul = gregoriantojd($intFirstMonth, $intFirstDay, $intFirstYear);
$intDate2Jul = gregoriantojd($intSecondMonth, $intSecondDay, $intSecondYear);

return $intDate1Jul - $intDate2Jul;

}//end Compare Date
echo compareDate ("01/03/2003","01/02/2003");
?>

Related Posts