Re: [請益] 計算扣掉六日的日期區間

看板PHP作者 (任性)時間13年前 (2010/12/17 15:41), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/5 (看更多)
※ 引述《JYHuang (夏天到了,冷不起來了說)》之銘言: : 因為資料是從陣列來的 : 如果要去計算期間有幾個週未日,似乎會滿秏時間的。 因為耗時間才要交給機器做, 不過好的作法可以讓機器少耗一些時間就是了... : 想推一下公式,卻又想不出關聯 @.@ : 請問有沒有人寫過類似的函式? 看你的資料的複雜度吧, 1. 只有日期,而且連續,那每七天扣掉兩天就是你的答案。 2. 只有日期,而且資料不連續的話,只好一筆一筆檢查,看該筆是否為週末或是週日。 檢查方法: 1. 先找出該天是哪一個 weekday (從 0-6 代表週日到週六) 2. 進行比對 至於效能方面不用太擔心,這種計算不會很花時間 以我的筆電,比對 10,000 次花 0.3 秒而已。 roga@carlisten-lx:~/test$ time php date.php real 0m0.371s user 0m0.340s sys 0m0.020s 程式內容 <?php function check_date($test_date) { // $test_date = '2011-02-13'; $date = new DateTime($test_date); $weekday = (int) $date->format('w'); $result = (in_array($weekday, array(0, 6))) ? TRUE : FALSE; // echo "$test_date is ". ($result ? 'weekend' : 'not weekend'); } for($i = 0; $i < 10000; $i++) { $test_date = rand(2001, 2010) . '-' . rand(1,12) . '-' . rand(1,28); check_date($test_date); } -- The Internet: where men are men, women are men, and children are FBI agents. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.89.121.16

12/17 17:29, , 1F
推!
12/17 17:29, 1F

12/28 01:01, , 2F
推!
12/28 01:01, 2F
※ 編輯: roga 來自: 202.89.121.16 (04/13 16:56)
文章代碼(AID): #1D2nGJCL (PHP)
討論串 (同標題文章)
文章代碼(AID): #1D2nGJCL (PHP)