Need a judgment function that returns whether the time is legitimate, PHP

Source: Internet
Author: User
    1. A function isvaliddate ($date) needs to be written, as follows:

function isValidDate($date){    //1. $date 是本周时 +  time()要在$date前一天的18:00之前 = true        //2. $date 为下周时 +  time()要在本周四下午六点后  = true        //3. 其余返回false.    (注:一周从周一开始)        $orderTime = strtotime($date);    $now = time();    if(date('W',$orderTime) == date('W',$now) && (strtotime($date,$now) - $now) > 86400/4)   //预订前一天的18:00,截止预订    {            return true;    }    if(date('W',$orderTime) == date('W',$now) + 1 && $now > strtotime('saturday 18:05 -2 day',$now)) //预订第二周,周四下午六点及之后    {        return true;    }        return false;}

Among them, when I write the second condition, I find that the time of the coverage seems to be a bit of a problem, I would like to see the views of you ha.

Reply content:

    1. A function isvaliddate ($date) needs to be written, as follows:

function isValidDate($date){    //1. $date 是本周时 +  time()要在$date前一天的18:00之前 = true        //2. $date 为下周时 +  time()要在本周四下午六点后  = true        //3. 其余返回false.    (注:一周从周一开始)        $orderTime = strtotime($date);    $now = time();    if(date('W',$orderTime) == date('W',$now) && (strtotime($date,$now) - $now) > 86400/4)   //预订前一天的18:00,截止预订    {            return true;    }    if(date('W',$orderTime) == date('W',$now) + 1 && $now > strtotime('saturday 18:05 -2 day',$now)) //预订第二周,周四下午六点及之后    {        return true;    }        return false;}

Among them, when I write the second condition, I find that the time of the coverage seems to be a bit of a problem, I would like to see the views of you ha.

How do you feel this is going to force someone to do an interview question? Since the hierarchy can be listed 123, the implementation should not be a problem ...

Installing Carbon

use Carbon\Carbon;/** * 校验日期 * @param  string  $date 日期 * @return boolean        */function isValidDate($date){    // $date 是本周时 +  time()要在$date前一天的18:00之前 = true    if (Carbon::parse($date)->format('W') == Carbon::now()->format('W') &&        time() < Carbon::parse($date)->subDay(1)->hour(18)->minute(0)->timestamp    ) {        return true;    }    // $date 为下周时 +  time()要在本周四下午六点后  = true    elseif (        Carbon::parse($date)->format('W') == Carbon::now()->addWeek(1)->format('W') &&        time() > Carbon::now()->startOfDay()->addDay(3)->hour(18)->minute(0)->timestamp    ) {        return true;    }    return false;}

Simply change the title of the masterif 语句 return

function isValidDate($date){    //1. $date 是本周时 +  time()要在$date前一天的18:00之前 = true        //2. $date 为下周时 +  time()要在本周四下午六点后  = true        //3. 其余返回false.    (注:一周从周一开始)        $orderTime = strtotime($date);    $now = time();    if(date('W',$orderTime) === date('W',$now)) // 当前周    {            // time()要在$date前一天的18:00之前 = true        return $now < strtotime(date('Y-m-d 18:00:00',strtotime("$date -1 day")));    }    if(date('W',$orderTime) === (date('W',$now) + 1)) // 下周    {        //  time()要在本周四下午六点后  = true        return $now > strtotime(date('Y-m-d 18:00:00',strtotime( '+'. 4-date('w') .' days' )));    }    return false;}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.