开发者

validating date range search in php

Im going to implement a search function in mySQL for an application Im building, the user types [for now, later will be a calendar picker] start date and end date, and click search

  1. if (start day > end day), THEN error
  2. if (start month > end month), THEN error

Ok so far, but when having into account that

  1. if (start day > end day) && (start month < end month), THEN search
  2. if (start Month > end Month) && (start year < end year), THEN search

I dont worry about leap years, I will loop all months to 31 days, because if the day is in the db, it will fetch it, other wise, it will go to 31 and return nothing as there is no day,

Im using varchar for my dates (no timestamp), as they are imported from json [iOS]

Ok, hope to make sense,

here the code,

<?PHP
  $start = $_POST['start_date'];
  $end = $_POST['end_date'];
  $start_time = explode('/', $start);
  $end_time = explode('/', $end);
   $count_start = $start_time[0];
  $count_end = $end_time[0];
  $month_start = $start_time[1];
  $month_end = $end_time[1];
  $year_start = $start_time[2];
  $year_end = $end_time[2];

  function cuenta($count_start, $count_end) {

for($count_start; $count_start <= $count_end; $count_start++) {

    print $count_start . "<BR>";
}

     }

     if (!isset($_POST['Submit1']) || ($start == "Start Date" && $end == "End Date") || ($start == "" && $end == "End Date") || ($start == "Start Date" && $end == "")
|| ($start == "" && $end == "")) 

    {

print ("no data yet")."<BR>";
    }

    if ($year_start[2] > $year_end[2]){

print ("Please make sure end date Year is equal or greater than start date Year");
}

    if (($month_start > $month_end) && ($year_start <= $year_end)){

print ("Please make sure end date Month is greater than start date Month");

     }
     elseif (($month_start > $month_end) && ($year_start < $year_end)){

cuenta($count_start, $count_end);           
     }

    elseif ($count_start > $c开发者_如何学Goount_end) {
print ("Please make sure end date Day is greater than start date Day");

    }


    ?>

Im beggining in php, Sorry if I miss the obvious!, [that is why im asking haha] so if im missing some other important validation for searching between a date range, plz let me know, and also plz point me in the best direction for this forest of ifs!

thanks a lot!


Its much easier to convert the dates to unix timestamps and then compare the integers. PHP has the function strtotime for this purpose. If you have the dates already split, you could use mktime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜