Yii2 rules 添加时间比较功能

php比较类文件:yiisoftyii2validatorsCompareValidator.php

JS比较类文件: yiisoftyii2assetsyii.validation.js

原来的比较 只包含integer 和 string 两种情况

通过添加类型 来增加时间的比较

前台用的是js时间选择插件 时间格式为 YYYY-hh-dd hh:ii:ss 之类的

PHP中用的是转换为时间戳比较时间   strtotime()

JS中 用的是 new Date() 比较时间(一定要 new 否则可能出问题)


// safari 浏览器 只能 new Date('yyyy/mm/dd')
function formatDate(value)
{
  value = value.split('-');
  if(value.length < 3) value.push('01');
  return value.join('/');
}


if
(options.type === 'number') { value = parseFloat(value); compareValue = parseFloat(compareValue); }else if(options.type === 'strtotime'){ // 这里是新添加的 //value = new Date(value); // compareValue = new Date(compareValue);

      value = new Date(formatDate(value));
      compareValue = new Date(formatDate(compareValue));


}
if ($type === 'number') {
    $value = (float) $value;
    $compareValue = (float) $compareValue;
}elseif($type === 'strtotime'){  // 这里是新添加的
    $value = strtotime($value);
    $compareValue = strtotime($compareValue);
} else {
    $value = (string) $value;
    $compareValue = (string) $compareValue;
}

更新JS文件后 一定要删除缓存哦!

原文地址:https://www.cnblogs.com/cgjcgs/p/6050420.html