需要一個判斷函數,返回是否合法時間,PHP

來源:互聯網
上載者:User
  1. 需要寫一個函數isValidDate($date), 條件如下:

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

其中,我寫第二個條件的時候,發現條件覆蓋的時間好像有點問題,想看下各位的見解哈。

回複內容:

  1. 需要寫一個函數isValidDate($date), 條件如下:

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

其中,我寫第二個條件的時候,發現條件覆蓋的時間好像有點問題,想看下各位的見解哈。

怎麼感覺這是要強行給別人做面試題的呢?既然條理都能列 123 了,實現應該不是問題吧。。。

安裝 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;}

簡單改一下題主的 if 語句 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;}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.