Home  >  Article  >  Backend Development  >  下头这些代码能不能简写啊

下头这些代码能不能简写啊

WBOY
WBOYOriginal
2016-06-13 11:47:30854browse

下面这些代码能不能简写啊?
下面这段代码有没有简单办法实现啊,我觉得这样写太复杂了,如果写一个月的数据,岂不是需要写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: 引用:

看不出来你想要做什么
能不能说明一下你的需求是什么?

我从数据库里面得到了本周所有的数据,然后分别判断每天是多少条,比如今天是周四,我从数据库中得到的是周一到周四的数据,然后分别得到周一多少数据,周二多少,周三多少,周四多少,如果今天是周日,我得到了本周所有的数据,然后分别得到周一周二周三周四等是多少。以后还要扩展到月,得到每天的。
Statement:
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