การหาว่า วันที่ อยู่ระหว่างวันที่ ที่กำหนดหรือไม่

เทคนิคการใช้ฟังก์ชั่นเกี่ยวกับวันที่ของ PHP ในการตรวจสอบวันที่ ว่าอยู่ในช่วงที่ต้องการหรือไม่ ด้วย PHP

<?
function dateBetween($start,$end,$theDate)
{
     $startES=dateAryEStime($start);
     $endES=dateAryEStime($end);
     $findES=dateAryEStime($theDate);
     return ($startES <= $findES && $findES <= $endES);
}

function dateAryEStime($theDate) {
     return dateEStime($theDate[0],$theDate[1],$theDate[2]);
}

function dateEStime($theDay,$theMonth,$theYear) {
     return mktime(0,0,0,$theMonth,$theDay,$theYear);
}
?>

การใช้งาน

<?
     $startDate=array(1,1,2006); //1-1-2006 วันที่-เดือน-คศ.
     $endDate=array(1,1,2007); //1-1-2007 วันที่-เดือน-คศ.

     $find[0]=array(1,8,2007); //1-8-2007 วันที่-เดือน-คศ.
     $find[1]=array(4,8,2006); //4-8-2006 วันที่-เดือน-คศ.

     $esTime1=dateAryEStime($startDate);
     $esTime2=dateAryEStime($endDate);
     $date1=date("M-d-Y",$esTime1);
     $date2=date("M-d-Y",$esTime2);

     for($i=0; $i < 2; $i++)
     {
          $date3=date("M-d-Y",dateAryEStime($find[$i]));
          if(dateBetween($startDate,$endDate,$find[$i])) {
                print("$date3 อยู่ระหว่าง วันที่ $date1 และวันที่ $date2<br />");
          } else {
               print("$date3 ไม่ได้อยู่ระหว่าง วันที่ $date1 และวันที่ $date2 <br />");
          }
     }
?>
ผู้เขียน goragod โพสต์เมื่อ 02 เม.ย. 2551 เปิดดู 8,259 ป้ายกำกับ PHP
^