CodeIgniter less_than 和 greater_than 验证不适用于时间

CodeIgniter less_than and greater_than validation does not work with time

我选择了一个旧的 CI 项目并迁移到 3.0.6,现在大部分内容都稳定了,除了 less_than 和 [=18= 的验证规则] 已被使用。

这是规则的非验证部分(其他规则工作正常):

$this->form_validation->set_rules('start', 'Start time', 'required|less_than[end]');
$this->form_validation->set_rules('end', 'End time', 'required|greater_than[start]');

以下是时间下拉列表的生成方式:

echo form_dropdown('start', $start_end_options, $start); 
echo form_dropdown('end', $start_end_options, $end);

这里是html生成的:

<select name="start">
<option value="00:00:00">00:00<option>
<option value="00:15:00">00:15<option>
<option value="00:30:00">00:30<option>
<option value="00:45:00">00:45<option>
<option value="01:00:00">01:00<option>
...code omitted...
</select>

<select name="end">
<option value="00:00:00">00:00<option>
<option value="00:15:00">00:15<option>
<option value="00:30:00">00:30<option>
<option value="00:45:00">00:45<option>
<option value="01:00:00">01:00<option>
...code omitted...
</select>

这里可能出了什么问题?

非常感谢任何帮助或指导。

问题在于验证例程 (less_thangreater_than) 需要数字或数字字符串。在值字符串中带有冒号 (:) 的它们不是数字字符串。

如果您为 value 使用时间戳,您需要的验证例程将起作用。

使用函数 strtotime("time_sting") 转换为时间戳。

strtotime("00:00:00") returns 1461733200.

这是您问题中的值的时间戳

"00:00:00" = 1461733200
"00:15:00" = 1461734100
"00:30:00" = 1461735000
"00:45:00" = 1461735900
"01:00:00" = 1461736800

用于<select>

<select name="start">
  <option value="1461733200">00:00<option>
  <option value="1461734100">00:15<option>
  <option value="1461735000">00:30<option>
  <option value="1461735900">00:45<option>
  <option value="1461736800">01:00<option>
</select>

less_thangreater_than 将适用于上述内容。

您也可以编写自己的验证方法来处理 value 中带有冒号的字符串。

  public function timestring_less_than($str, $max)
  {
    return strcmp($str, $max) < 0 ? TRUE : FALSE;
  }

如果字符串相等,上面的也会 return FALSE

补充验证方法可以很容易地定义。

  public function timestring_greater_than($str, $max)
  {
    return strcmp($str, $max) > 0 ? TRUE : FALSE;
  }