A judgment function is needed to return whether the time is legal or not, PHP

WBOY
Release: 2023-03-01 21:46:01
Original
1312 people have browsed it

  1. You need to write a function isValidDate($date), the conditions are as follows:

<code>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;

}</code>
Copy after login
Copy after login

Among them, when I wrote the second condition, I found that there seemed to be a problem with the time covered by the condition. I would like to see your opinions.

Reply content:

  1. You need to write a function isValidDate($date), the conditions are as follows:

<code>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;

}</code>
Copy after login
Copy after login

Among them, when I wrote the second condition, I found that there seemed to be a problem with the time covered by the condition. I would like to see your opinions.

Why do you feel like you are forcing yourself to do interview questions for others? Since I can list 123 in an orderly manner, implementation should not be a problem. . .

Install Carbon

<code class="php">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;
}</code>
Copy after login

Simply change the if statement in the title of the question return

<code>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;

}</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template