php根据时间段查询每天的数据
现在需要查询给定的日期内的每天的数据,请教各位有没有什么好的办法?
回复讨论(解决方案)
首先数据库必须要有一个保存日期的字段,然后按日期分组统计即可。
首先数据库必须要有一个保存日期的字段,然后按日期分组统计即可。
你好,我的数据库保存的有unix时间戳形式的日期字段,现在是给定一个开始时间和结束时间,我要怎么才能分别查出每天的数据的?
时间戳分组比较麻烦,先用日期转换函数转成日期,然后再分组,不过这样效率较低,最好还是设计的时候增加一个日期字段。
能不能详细说下的,我还是不大明白哈!
你可以看看MYSQL的日期函数,比如你保存时间戳的字段为t
可以这样统计数据
SELECT COUNT(*) AS n,FROM_UNIXTIME(`t`,'%y-$m-%d') AS d FROM `tb` GROUP BY d ORDER BY d
不过这种效率很低,最好你插入的时候就计算好日期。
select id,FROM_UNIXTIME(timeField, '%Y%m%d') as time from tableName where FROM_UNIXTIME(timeField, '%Y%m%d')=20150920
你好,“插入的时候就计算好日期”指的是在数据库保存数据的时候就保存成如2015-10-08这种格式的日期吗?
我现在获取到的是一个时间段的开始时间和结束时间,是不是要先把这段时间按天保存到一个数组,然后按“每天”执行查询当天的数据的?
你好,“插入的时候就计算好日期”指的是在数据库保存数据的时候就保存成如2015-10-08这种格式的日期吗?
我现在获取到的是一个时间段的开始时间和结束时间,是不是要先把这段时间按天保存到一个数组,然后按“每天”执行查询当天的数据的?
是的,插入的时候就保存日期格式。
有开始时间和结束时间的话,查询的时候用大于小于比较符即可。如果数据量较大的话,每天写定时整理好存在一个新表中就快多了。
<?php ///$ary为该时间区间下所有的数据foreach($ary as $k=>$v){ $datas['day_'.date('md',$v['timestamp'])][]=$v;//分组 }var_dump($datas['day_0809'])//为8月9号那天所有数据;?>
你好,“插入的时候就计算好日期”指的是在数据库保存数据的时候就保存成如2015-10-08这种格式的日期吗?
我现在获取到的是一个时间段的开始时间和结束时间,是不是要先把这段时间按天保存到一个数组,然后按“每天”执行查询当天的数据的?
是的,插入的时候就保存日期格式。
有开始时间和结束时间的话,查询的时候用大于小于比较符即可。如果数据量较大的话,每天写定时整理好存在一个新表中就快多了。
明白了,多谢哈!
<?php ///$ary为该时间区间下所有的数据foreach($ary as $k=>$v){ $datas['day_'.date('md',$v['timestamp'])][]=$v;//分组 }var_dump($datas['day_0809'])//为8月9号那天所有数据;?>
3g油!
时间戳有时间戳的好处
用php来做数据整合就可以咯.
时间戳有时间戳的好处
用php来做数据整合就可以咯.
你好,我也是这样认为的,但是一直不太清楚什么情况用时间戳和日期格式比较合适!
PHP做数据整合适合小数据量,大数据太耗资源,如果你有100W数据需要占用多少内存多少带宽?一亿呢?
PHP做数据整合适合小数据量,大数据太耗资源,如果你有100W数据需要占用多少内存多少带宽?一亿呢?
怎么做才能最大程度的减少资源的消耗呢?
每种数据库的日期时间函数都是不一样的
如果你有升迁到大型或分布式数据库的打算时,则一定不能使用日期、时间类型字段
使用 unix 时间戳可免除绝大多数日期时间函数的烦恼
select *,from_unixtime(addtime, '%Y%m%d') as d from table where from_unixtime(addtime,'%Y%m%d')>=20150101 and from_unixtime(addtime,'%Y%m%d')<=20150201;
$d1 = strtotime('2015-01-01');
$d2 = strtotime('2015-02-01');
$sql = "select * from tbl_name where field between $d1 and $d2"
查询时没有了格式转换,自然效率要高很多
数据库存时间戳就挺好的 不用改 你查询的条件 可以把给的制定日期变成时间戳再去查库啊
多谢各位的回答,不但解决了我的问题还让我学到了更多的知识,非常感谢!!!
数据库存时间戳就挺好的 不用改 你查询的条件 可以把给的制定日期变成时间戳再去查库啊
数据库中保存的是时间戳,如果是要以每天为单位查询出一段时间每天的数据就不大方便了吧。
数据库表(时间字段应该是时间戳格式,为了便于显示直接写成date格式):
时间 | 种类 | 数量
2015-10-14 1 20
2015-10-14 2 15
2015-10-14 3 35
2015-10-15 3 35
要如下结果:
时间 | 销售种类总数
2015-10-14 3
2015-10-15 1
这没有什么
select 时间戳, sum(数量) from 表 group by floor(时间戳/86400)
这没有什么
select 时间戳, sum(数量) from 表 group by floor(时间戳/86400)
哇,还能这样用,受教了,多谢!!!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
