【问题标题】:InvalidArgumentException in Carbon::createFromDate()->ageCarbon::createFromDate()->age 中的 InvalidArgumentException
【发布时间】:2017-04-26 11:54:24
【问题描述】:

您好,我正在为我的 laravel 项目中的输入数据编写自定义验证。我正在使用 Carbon::createFromDate()->age 来获取用户年龄并检查他是否年满 16 岁。我认为我做得不好,因为我得到 InvalidArgumentException 出现此类错误:

  • 找不到分隔符
  • 找不到分隔符
  • 找不到两位数的分钟
  • 发现意外数据。
  • 跟踪数据
  • $rok 是一年(例如 1996 年)$miesiac 是一个月,$dzien 是一天。 Pesel 是波兰公民的唯一身份证号码。从 pesel 我得到日期(年、月、日)

    我得到了一些大数字,但我不知道它们是什么意思这里是 dd:“年:21586738427 月:1900167 日:9001727”

    这是我的 AppServiceProvider 的代码

        <?php
    
    namespace App\Providers;
    use Illuminate\Support\ServiceProvider;
    use Carbon\Carbon;
    use Validator;
    use Log;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            //Custom validator for pesel validation
            Validator::extend('pesel',function($attribute,$value,$parameters)
            {
              $v = $value;
              //check if psesel is 11 chars long
              if (strlen($v) != 11)
              {
                Log::info("Pesel is not 11 chars long");
                return;
              }
              //check whether all chars are numbers
    
              $aInt = array();
    
              for($i = 0; $i < 11; $i++)
              {
               $val = substr($v,$i+1);
               $valInt = (int) $val;
               $aInt[$i] = $valInt;
             if(is_nan($aInt[$i]))
                {
                    Log::info("User inserted invalid number");
                    return;
                }
              }
    
    
              //check control sum
              $wagi = [1,3,7,9,1,3,7,9,1,3,1];
              $sum = 0;
              for($i = 0;$i < 11;$i++)
              {
                $sum += $wagi[$i]*$aInt[$i];
                if(Log::info(($sum%10)!=0))
                {
                  return;
                }
              //count the year,month,and day from pesel
              $rok = 1900+$aInt[0]*10+$aInt[1];
              if($aInt[2]>=2 && $aInt[2]<8)
              {
                $rok += floor($aInt[2]/2)*100;
              }
              if($aInt[2]>=8)
              {
                $rok -= 100;
              }
              $miesiac = ($aInt[2]%2)*10+$aInt[3];
              $dzien = $aInt[4]*10+$aInt[5];
              Log::info("Parsing the date in carbon");
    
              //validate whether user is 16 years or more
              $userAge =  Carbon::createFromDate($rok, $miesiac, $dzien,'Europe/Warsaw')->age;
              if($userAge < 16)
              {
                Log::info("user is not 16 or more");
                return;
              }
    
    
    
            }
    
          });
        }
    
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    
    }
    

    【问题讨论】:

    • 你能指定$rok$miesiac$dizen的值吗?
    • $rok 是一年(例如 1996 年)$miesiac 是一个月,$dzien 是一天。 Pesel 是波兰公民的唯一身份证号码。从 pesel 我得到日期(年、月、日)
    • 您确定吗,可以dd() 全部 3 个,并将结果添加到您的问题中!
    • 你说得对,我得到了一些大数字,但我不知道它们是什么意思这里是 dd:“年:21586738427 月:1900167 日:9001727”

    标签: php laravel validation date php-carbon


    【解决方案1】:

    好的,我解决了这个问题。这是我的代码的错

      $val = substr($v,$i+1); 
    

    错了 应该是

      $val = substr($v,$i,1);
    

    现在可以了

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 2020-02-19
      • 2014-08-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 2020-04-29
      相关资源
      最近更新 更多