当前位置:Gxlcms > PHP教程 > 下部这些代码能不能简写啊

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

时间:2021-07-01 10:21:17 帮助过:24人阅读

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

分享到: 更多

人气教程排行