search
HomeBackend DevelopmentPHP Tutorial 下头这些代码能不能简写啊

下面这些代码能不能简写啊?
下面这段代码有没有简单办法实现啊,我觉得这样写太复杂了,如果写一个月的数据,岂不是需要写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 />
?>
如果你从数据库得到的是一周的数据
那么,设 $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
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft