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

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

WBOY
Release: 2016-06-13 12:29:45
Original
1003 people have browsed it

下面这些代码能不能简写啊?
下面这段代码有没有简单办法实现啊,我觉得这样写太复杂了,如果写一个月的数据,岂不是需要写30个case
求大侠帮忙,俺是新手

<?php<br />
date_default_timezone_set('PRC');<br />
$c1=0;<br />
$c2=0;<br />
$c3=0;<br />
$c4=0;<br />
$c5=0;<br />
$c6=0;<br />
$c7=0;<br />
$w = date("w",time());<br />
switch($w){<br />
	case 1: <br />
		<br />
		break; <br />
	case 2:<br />
	    $t1=strtotime(date("Y-m-d")." 23:59:59");//周二<br />
		$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周一<br />
		if($t2<1387420136&&1387420136<=$t1){<br />
			$c1 += 1;<br />
		}elseif(1387420136<=$t2){<br />
			$c2 += 1;<br />
		}<br />
		echo $c1;<br />
		break; <br />
	case 3:<br />
	    $t1=strtotime(date("Y-m-d")." 23:59:59");//周三<br />
		$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周二<br />
		$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周一<br />
		if($t2<1387420136&&1387420136<=$t1){<br />
			$c1 += 1;<br />
		}elseif($t3<1387420136&&1387420136<=$t2){<br />
			$c2 += 1;<br />
		}elseif(1387420136<=$t3){<br />
			$c3 += 1;<br />
		}<br />
		echo $c1;		<br />
		break; <br />
	case 4:<br />
	    $t1=strtotime(date("Y-m-d")." 23:59:59");//周四<br />
		$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周三<br />
		$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周二<br />
		$t4=strtotime(date("Y-m-d",strtotime("-3 day"))." 23:59:59");//周一<br />
		if($t2<1387420136&&1387420136<=$t1){<br />
			$c1 += 1;<br />
		}elseif($t3<1387420136&&1387420136<=$t2){<br />
			$c2 += 1;<br />
		}elseif($t4<1387420136&&1387420136<=$t3){<br />
			$c3 += 1;<br />
		}elseif(1387420136<=$t4){<br />
			$c4 += 1;<br />
		}<br />
		echo $c1;<br />
		break; <br />
	case 5:<br />
		$t1=strtotime(date("Y-m-d")." 23:59:59");//周五<br />
		$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周四<br />
		$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周三<br />
		$t4=strtotime(date("Y-m-d",strtotime("-3 day"))." 23:59:59");//周二<br />
		$t5=strtotime(date("Y-m-d",strtotime("-4 day"))." 23:59:59");//周一<br />
		if($t2<1387420136&&1387420136<=$t1){<br />
			$c1 += 1;<br />
		}elseif($t3<1387420136&&1387420136<=$t2){<br />
			$c2 += 1;<br />
		}elseif($t4<1387420136&&1387420136<=$t3){<br />
			$c3 += 1;<br />
		}elseif($t5<1387420136&&1387420136<=$t4){<br />
			$c4 += 1;<br />
		}elseif(1387420136<=$t5){<br />
			$c5 += 1;<br />
		}<br />
		echo $c1;<br />
		break; <br />
	case 6:<br />
		$t1=strtotime(date("Y-m-d")." 23:59:59");//周六<br />
		$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周五<br />
		$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周四<br />
		$t4=strtotime(date("Y-m-d",strtotime("-3 day"))." 23:59:59");//周三<br />
		$t5=strtotime(date("Y-m-d",strtotime("-4 day"))." 23:59:59");//周二<br />
		$t6=strtotime(date("Y-m-d",strtotime("-5 day"))." 23:59:59");//周一<br />
		if($t2<1387420136&&1387420136<=$t1){<br />
			$c1 += 1;<br />
		}elseif($t3<1387420136&&1387420136<=$t2){<br />
			$c2 += 1;<br />
		}elseif($t4<1387420136&&1387420136<=$t3){<br />
			$c3 += 1;<br />
		}elseif($t5<1387420136&&1387420136<=$t4){<br />
			$c4 += 1;<br />
		}elseif($t6<1387420136&&1387420136<=$t5){<br />
			$c5 += 1;<br />
		}elseif(1387420136<=$t6){<br />
			$c6 += 1;<br />
		}<br />
		echo $c1;<br />
		break; <br />
	case 7:<br />
	    $t1=strtotime(date("Y-m-d")." 23:59:59");//周日<br />
		$t2=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");//周六<br />
		$t3=strtotime(date("Y-m-d",strtotime("-2 day"))." 23:59:59");//周五<br />
		$t4=strtotime(date("Y-m-d",strtotime("-3 day"))." 23:59:59");//周四<br />
		$t5=strtotime(date("Y-m-d",strtotime("-4 day"))." 23:59:59");//周三<br />
		$t6=strtotime(date("Y-m-d",strtotime("-5 day"))." 23:59:59");//周二<br />
		$t7=strtotime(date("Y-m-d",strtotime("-6 day"))." 23:59:59");//周一<br />
		if($t2<1387420136&&1387420136<=$t1){<br />
			$c1 += 1;<br />
		}elseif($t3<1387420136&&1387420136<=$t2){<br />
			$c2 += 1;<br />
		}elseif($t4<1387420136&&1387420136<=$t3){<br />
			$c3 += 1;<br />
		}elseif($t5<1387420136&&1387420136<=$t4){<br />
			$c4 += 1;<br />
		}elseif($t6<1387420136&&1387420136<=$t5){<br />
			$c5 += 1;<br />
		}elseif($t7<1387420136&&1387420136<=$t6){<br />
			$c6 += 1;<br />
		}elseif(1387420136<=$t7){<br />
			$c7 += 1;<br />
		}<br />
		echo $c1;<br />
		break;<br />
	default:;			    <br />
}<br />
?>
Copy after login
如果你从数据库得到的是一周的数据
那么,设 $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: 引用:

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

我从数据库里面得到了本周所有的数据,然后分别判断每天是多少条,比如今天是周四,我从数据库中得到的是周一到周四的数据,然后分别得到周一多少数据,周二多少,周三多少,周四多少,如果今天是周日,我得到了本周所有的数据,然后分别得到周一周二周三周四等是多少。以后还要扩展到月,得到每天的。
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template