【问题标题】:validating date range search in php在php中验证日期范围搜索
【发布时间】:2011-06-09 05:32:36
【问题描述】:

我将在 mySQL 中为我正在构建的应用程序实现搜索功能, 用户输入 [现在,以后将是日历选择器] 开始日期和结束日期, 然后点击搜索

  1. 如果(开始日 > 结束日),则错误
  2. 如果(开始月份 > 结束月份),THEN 错误

到目前为止还可以,但是考虑到这一点

  1. if (start day > end day) && (start month
  2. if (start Month > end Month) && (start year

我不担心闰年,我会将所有月份循环到 31 天,因为如果这一天在数据库中,它将获取它,否则它将转到 31 并且没有返回任何内容,因为没有一天,

我使用 varchar 作为我的日期(没有时间戳),因为它们是从 json [iOS] 导入的

好吧,希望有道理,

这里是代码,

<?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 > $count_end) {
print ("Please make sure end date Day is greater than start date Day");

    }


    ?>

我开始使用 php,对不起,如果我错过了明显的!,[这就是我问哈哈的原因] 因此,如果我错过了在日期范围之间进行搜索的其他重要验证,请告诉我,并请为我指出这片 ifs 森林的最佳方向!

非常感谢!

【问题讨论】:

  • 嗨,tnx,没有错误,只是没有按预期工作

标签: php validation search date


【解决方案1】:

将日期转换为 unix 时间戳然后比较整数要容易得多。为此,PHP 具有 strtotime 函数。如果您已经拆分了日期,则可以使用 mktime。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-06
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多