Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 下面这些代码能不能简写啊?

下面这些代码能不能简写啊?

Jun 23, 2016 pm 02:39 PM

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

<?phpdate_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:;			    }?>
Copy after login


回复讨论(解决方案)

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

看不出来你想要做什么
能不能说明一下你的需求是什么?
我从数据库里面得到了本周所有的数据,然后分别判断每天是多少条,比如今天是周四,我从数据库中得到的是周一到周四的数据,然后分别得到周一多少数据,周二多少,周三多少,周四多少,如果今天是周日,我得到了本周所有的数据,然后分别得到周一周二周三周四等是多少。以后还要扩展到月,得到每天的。
我上面的程序的意思是
先得到所有的从数据库中查询出来的数据。然后初始每天的数量是0,到时候if判断放到循环数据里面,分别判断时间属于哪天,然后相应的天的数量+1,最后把所有的数据输出即可。
或者请问还有别的简单点的实现办法吗?需要考虑进数据库的压力和程序执行速度。

用递归。。。

如果你从数据库得到的是一周的数据
那么,设 $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 中就是每天的计数量




看不出来你想要做什么
能不能说明一下你的需求是什么?
我从数据库里面得到了本周所有的数据,然后分别判断每天是多少条,比如今天是周四,我从数据库中得到的是周一到周四的数据,然后分别得到周一多少数据,周二多少,周三多少,周四多少,如果今天是周日,我得到了本周所有的数据,然后分别得到周一周二周三周四等是多少。以后还要扩展到月,得到每天的。
我上面的程序的意思是
先得到所有的从数据库中查询出来的数据。然后初始每天的数量是0,到时候if判断放到循环数据里面,分别判断时间属于哪天,然后相应的天的数量+1,最后把所有的数据输出即可。
或者请问还有别的简单点的实现办法吗?需要考虑进数据库的压力和程序执行速度。
如果只是增加相应的天是数量的话,这个很简单吧
直接用数组保存就可以了
如果你是按星期几来增加的话,这个很简单:
直接用数组$days=array(0,0,0,0,0,0,0);//根据date('w', $r['date']) - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六)  而得出的一个数组

这样
你每次只需要
$days[date('w', 你的日期)]++; 就可以省略你判断了

如果你是按月的话,那么你定义个数组下标是1-31的数组,初始值都是0的数组,其他的就跟周的一样了

如果你从数据库得到的是一周的数据
那么,设 $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 中就是每天的计数量
谢谢,已经解决了,6楼的chinmo版主也是这个思路,真是英雄所见略同,谢谢!

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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles