Yii2 rules 添加時間比較功能


php比較類文件:yiisoft\yii2\validators\CompareValidator.php

JS比較類文件: yiisoft\yii2\assets\yii.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文件后 一定要刪除緩存哦!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM