douxuan0698 2017-05-12 08:27
浏览 30
已采纳

如何阻止超过4位数的年份?

The situation, I use twig and when a user put by mistake a date like 15.05.20177 (European format, we use only this one) twig can't parse the date.

("DateTime::__construct(): Failed to parse time string (12.04.20177) at position 10 (7): Unexpected character").

I have tried to preg_match, string compare, date compare but nothing works...

        if (!preg_match("/^[0-9]{2}.[0-9]{2}.[0-9]{4}$/", $date)) {
            throw new RuntimeException("Bug of year 10'000.");
        }

or

        $dateToCompare = strtotime($date);
        $maxDate = strtotime("31.12.9999");
        if ($dateToCompare > $maxDate) {
            throw new RuntimeException("Bug of year 10'000.");
        }

or

        if ($date > strtotime("31.12.9999")) {
            throw new RuntimeException("Bug of year 10'000.");
        }

The fix can be add here:

        $this->twig->addFilter(new Twig_SimpleFilter('date', function($date, $formatOrOptions = null, $timezone = null) {

        // Handle a larger option array but keep the backward compatibility with the Twig date helper
        $defaults = [
            'format' => null,
            'timezone' => null,
            'empty_output' => ''
        ];
        if ($formatOrOptions == null || is_string($formatOrOptions)) {
            $options = array_merge($defaults, ['format'=>$formatOrOptions, 'timezone'=> $timezone]);
        }
        elseif (is_array($formatOrOptions)) {
            if ($diff = array_diff(array_keys($formatOrOptions), array_keys($defaults))) {
                throw new RuntimeException("Invalid options: [".implode(', ', $diff)."]");
            }
            $options = array_merge($defaults, $formatOrOptions);
        }
        else {
            throw new RuntimeException("First filter parameter must be a string or an array");
        }

        // This is because by default Twig return the today date for null values
        if ($date == null || $date == '' || $date == '0000-00-00'){
            return $options['empty_output'];
        }

        return twig_date_format_filter($this->twig, $date, $options['format'], $options['timezone']);

Thanks, and sorry for the "stupid" question :)

  • 写回答

1条回答 默认 最新

  • dr5779 2017-05-12 08:41
    关注

    Instead of passing the string to Twig, you can try and pass a DateTime Object. This way, the date format validation can be done in the PHP code directly. Before passing the variable to your Twig environment, you should try and build the DateTime Object like so :

    $myDate = DateTime::createFromFormat('d.m.Y', $stringDate);
    

    You should then check the contents of the date variable. If it's false, then the parsing failed. Check it this way :

    // Do a strict equality check here
    if ($myDate === false) {
        throw new RuntimeException("Invalid date");
    }
    

    Of course, handle your exception in the way you best see fit according to your needs. This is just an example

    You can then pass the $myDate variable instead of the string and integrate it in your twig template

    {{ myDate|date(d.m.Y) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教