Home  >  Q&A  >  body text

How to write this verification rule?

Suppose you want to verify whether the date field is between a certain date and today. What is the simplest way to write verification rules?

Example: $rule = [

'birthday' => 'after:1940-1-1|before:today',

];

How should "today" in the rules be obtained and inserted into the rules?

phpcn_u16587phpcn_u165872570 days ago1894

reply all(2)I'll reply

  • phpcn_u16587

    phpcn_u165872017-09-09 11:57:41

    The purpose of my question is whether it is possible to insert a custom variable or function into the "today" position?

    reply
    0
  • 按键盘手指磨破皮

    按键盘手指磨破皮2017-09-09 11:04:23

    mktime(0,0,0,date('m'),date('d'),date('Y')); //今日凌晨
    mktime(0,0,0,date('m'),date('d')-1,date('Y')); //昨日凌晨
    mktime(0,0,0,date('m'),date('d')+1,date('Y')); //明日凌晨
    前面3个从左到右分别代表小时,分钟,秒
    mktime(12,9,10,date('m'),date('d'),date('Y')); //今日的12:09:10
    上面获取到的都是时间戳格式 可以通过date转换为日期格式
    date("Y-m-d H:i:s",mktime(12,9,10,date('m'),date('d'),date('Y')));
    当然也可以日期转时间戳
    strtotime("2011-2-18 08:15:42"); //时间转时间戳
    那么知道以上时间的获取方式,再根据你的需求来进行运算,用时间戳来运算就比较简单了
    昨日的时间戳肯定小于今日的时间戳
    明日的时间戳肯定大于今日的时间戳
    比如你要判断2000-1-1 是否再1940-1-1 和今日之间 可以这样写
    $a = strtotime("2000-1-1");
    $b = time();//当前的时间戳 你要指定时间也可以用上面的一些函数
    $m = strtotime("1940-1-1");
    if($a > $m && $a < $b)
    {
        echo '在1940-今日之间';
    }else{
        echo '不是指定的时间段';
    }
    望采纳


    reply
    1
  • Cancelreply