search
HomeBackend DevelopmentPHP Tutorial php 依据输入 开始时间 结束时间 获取数据库中表中记录

php 根据输入 开始时间 结束时间 获取数据库中表中记录
我要做的是一个统计页面 查询条件是根据输入(js点击选择时间)开始时间 结束时间 点确定按钮查询这时间段记录
数据库字段:
id int(10)
ip varchar(50)
time varchar(50)
type varchar(50) 

开始日期:  结束日期:  

下面是一个表格:
内容是类型和对应数量

请各位帮下忙,刚接触统计




------解决方案--------------------
开始日期:timeStart" onfocus="c.showMoreDay = false;c.show(this);" />  结束日期:timeEnd" onfocus="c.showMoreDay = false;c.show(this);" />  

$t1=$_POST['timeStart'];<br />
$t2=$_POST['timeEnd'];<br />
$sql="select type,count(*) from tt where time BETWEEN '$t1' AND '$t2' group by type";<br />
$result=mysql_query($sql);<br />
while($row=mysql_fetch_row($result)){<br />
        echo $row[0].'  '.$row[1].'<br>';<br />
}

------解决方案--------------------
time varchar(50)
至少应该校正传入数据符合 time 字段的格式才能比较
他没说,我不知道
------解决方案--------------------
关键就是数据库语句,原理和做法就和你按ID查询一样的。SQL语句二楼已经给出来了。
另外一点的话,楼主可以去下载个时间选择插件,毕竟让用户手动输入时间是很不理想的。介绍一个jq的 jquery-calendar 网上有很多种样式表,可以自己选样式。
------解决方案--------------------
time varchar(50)
你保存的时间是字符串,那么
2012.12.14 10:43
2012/12/14 10:43:20
2012-12-14 10:43:31p
都是合法的日期
如果表单传入的是 2012/12/14 10:43:22
那么至少有两个是比配不到的



引用:
xuzuning 您说的不太懂是说插入时间之前先判断下格式吗
xiebuqing 我是下载的日历插件用户选择时间
还一个问题是当用户不查询时间段记录的时候 页面要默认显示数据库里所有类型记录

------解决方案--------------------
那你传入的日期格式是怎样的。
------解决方案--------------------
即便只是年月日
'2012/12/14' 与 '2012-12-14' 也是不相等的

------解决方案--------------------
只要格式能够统一,#1的代码就可以用了
------解决方案--------------------
判断一下就可以了
if(!empty($t1) && !empty($t2)){<br />
   $sql="select type,count(*) from tt where time BETWEEN '$t1' AND '$t2' group by type";<br />
}else{<br />
    $sql="select type,count(*) from total_counter group by type";<br />
}<br />
$result=mysql_query($sql);

------解决方案--------------------
这样写好些,能适应各种情况
$t1 = $_POST['timeStart'];<br>
$t2 = $_POST['timeEnd'];<br>
$t = array(1);<br>
if($t1) $r[] = "time>='$t1'";<br>
if($t2) $r[] = "time
$t = join(' and ', $t);<br>
<br>
$sql = "select type,count(*) from total_counter where $t group by type"; <div class="clear">
                 
              
              
        
            </div>
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool