下面这段代码有没有简单办法实现啊,我觉得这样写太复杂了,如果写一个月的数据,岂不是需要写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:; }?>
回复讨论(解决方案)
看不出来你想要做什么
能不能说明一下你的需求是什么?
看不出来你想要做什么
能不能说明一下你的需求是什么?
我从数据库里面得到了本周所有的数据,然后分别判断每天是多少条,比如今天是周四,我从数据库中得到的是周一到周四的数据,然后分别得到周一多少数据,周二多少,周三多少,周四多少,如果今天是周日,我得到了本周所有的数据,然后分别得到周一周二周三周四等是多少。以后还要扩展到月,得到每天的。
我上面的程序的意思是
先得到所有的从数据库中查询出来的数据。然后初始每天的数量是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版主也是这个思路,真是英雄所见略同,谢谢!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.