下頭這些代碼能不能簡寫啊

來源:互聯網
上載者:User
下面這些代碼能不能簡寫啊?
下面這段代碼有沒有簡單辦法實現啊,我覺得這樣寫太複雜了,如果寫一個月的資料,豈不是需要寫30個case
求大俠幫忙,俺是新手
date_default_timezone_set('PRC');
$c1=0;
$c2=0;
$c3=0;
$c4=0;
$c5=0;
$c6=0;
$c7=0;
$w = date("w",time());
switch($w){
case 1:

break;
case 2:
$t1=strtotime(date("Y-m-d")." 23:59:59");//周二
$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周一
if($t2<1387420136&&1387420136<=$t1){
$c1 += 1;
}elseif(1387420136<=$t2){
$c2 += 1;
}
echo $c1;
break;
case 3:
$t1=strtotime(date("Y-m-d")." 23:59:59");//周三
$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周二
$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周一
if($t2<1387420136&&1387420136<=$t1){
$c1 += 1;
}elseif($t3<1387420136&&1387420136<=$t2){
$c2 += 1;
}elseif(1387420136<=$t3){
$c3 += 1;
}
echo $c1;
break;
case 4:
$t1=strtotime(date("Y-m-d")." 23:59:59");//周四
$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周三
$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周二
$t4=strtotime(date("Y-m-d",strtotime("-3 day"))." 23:59:59");//周一
if($t2<1387420136&&1387420136<=$t1){
$c1 += 1;
}elseif($t3<1387420136&&1387420136<=$t2){
$c2 += 1;
}elseif($t4<1387420136&&1387420136<=$t3){
$c3 += 1;
}elseif(1387420136<=$t4){
$c4 += 1;
}
echo $c1;
break;
case 5:
$t1=strtotime(date("Y-m-d")." 23:59:59");//周五
$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周四
$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周三
$t4=strtotime(date("Y-m-d",strtotime("-3 day"))." 23:59:59");//周二
$t5=strtotime(date("Y-m-d",strtotime("-4 day"))." 23:59:59");//周一
if($t2<1387420136&&1387420136<=$t1){
$c1 += 1;
}elseif($t3<1387420136&&1387420136<=$t2){
$c2 += 1;
}elseif($t4<1387420136&&1387420136<=$t3){
$c3 += 1;
}elseif($t5<1387420136&&1387420136<=$t4){
$c4 += 1;
}elseif(1387420136<=$t5){
$c5 += 1;
}
echo $c1;
break;
case 6:
$t1=strtotime(date("Y-m-d")." 23:59:59");//周六
$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周五
$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周四
$t4=strtotime(date("Y-m-d",strtotime("-3 day"))." 23:59:59");//周三
$t5=strtotime(date("Y-m-d",strtotime("-4 day"))." 23:59:59");//周二
$t6=strtotime(date("Y-m-d",strtotime("-5 day"))." 23:59:59");//周一
if($t2<1387420136&&1387420136<=$t1){
$c1 += 1;
}elseif($t3<1387420136&&1387420136<=$t2){
$c2 += 1;
}elseif($t4<1387420136&&1387420136<=$t3){
$c3 += 1;
}elseif($t5<1387420136&&1387420136<=$t4){
$c4 += 1;
}elseif($t6<1387420136&&1387420136<=$t5){
$c5 += 1;
}elseif(1387420136<=$t6){
$c6 += 1;
}
echo $c1;
break;
case 7:
$t1=strtotime(date("Y-m-d")." 23:59:59");//周日
$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周六
$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周五
$t4=strtotime(date("Y-m-d",strtotime("-3 day"))." 23:59:59");//周四
$t5=strtotime(date("Y-m-d",strtotime("-4 day"))." 23:59:59");//周三
$t6=strtotime(date("Y-m-d",strtotime("-5 day"))." 23:59:59");//周二
$t7=strtotime(date("Y-m-d",strtotime("-6 day"))." 23:59:59");//周一
if($t2<1387420136&&1387420136<=$t1){
$c1 += 1;
}elseif($t3<1387420136&&1387420136<=$t2){
$c2 += 1;
}elseif($t4<1387420136&&1387420136<=$t3){
$c3 += 1;
}elseif($t5<1387420136&&1387420136<=$t4){
$c4 += 1;
}elseif($t6<1387420136&&1387420136<=$t5){
$c5 += 1;
}elseif($t7<1387420136&&1387420136<=$t6){
$c6 += 1;
}elseif(1387420136<=$t7){
$c7 += 1;
}
echo $c1;
break;
default:;
}
?>

------解決方案--------------------
如果你從資料庫得到的是一周的資料
那麼,設 $r['date'] 取回的時間資料,則有
$c = array(0,0,0,0,0,0,0);
//迴圈中
$c[date('w', $r['date'])]++;
$c 中就是每天的計數量

如果你從資料庫得到的是一月的資料
那麼,設 $r['date'] 取回的時間資料,則有
$c = array_fill(1, 31, 0);
//迴圈中
$c[date('d', $r['date'])]++;
$c 中就是每天的計數量




------解決方案--------------------
引用:
Quote: 引用:

看不出來你想要做什麼
能不能說明一下你的需求是什嗎?

我從資料庫裡面得到了本周所有的資料,然後分別判斷每天是多少條,比如今天是周四,我從資料庫中得到的是周一到周四的資料,然後分別得到周一多少資料,周二多少,周三多少,周四多少,如果今天是周日,我得到了本周所有的資料,然後分別得到周一周二周三周四等是多少。以後還要擴充到月,得到每天的。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.