php、mysql查询当天,查询本周,查询本月的数据实例(字段是时间戳)
php、mysql查询当天,查询本周,查询本月的数据实例(字段是时间戳)
//其中 video 是表名;
//createtime 是字段;
//
//数据库time字段为时间戳
//
//查询当天: $start = date('Y-m-d 00:00:00');
$end = date('Y-m-d H:i:s');
SELECT * FROM `table_name` WHERE `time` >= unix_timestamp( '$start' ) AND `time` <= unix_timestamp( '$end' ) //查询本周: SELECT yearweek( '2011-04-17 15:38:22',1 ) //结果是201115
SELECT yearweek( '2011-04-17 15:38:22' ) //结果是201116
//yearweek的第2个参数设置为1的原因是,中国人习惯把周1作为本周的第一天
//另外补充下:
//2011-04-17 是周日。
SELECT dayofweek( '2011-04-17 15:38:22' )// 查询出的是1,把礼拜天作为一周的第一天。
SELECT dayofweek( '2011-04-18 15:38:22' ) //查询出的是2
SELECT weekday( '2011-04-17 15:38:22' )// 查询出的是6,
SELECT weekday( '2011-04-18 15:38:22' )// 查询出的是0,
//所以建议使用weekday,查询出来的结果+1就可以了,就比较符合国人的习惯了。 SELECT * FROM `table_name` WHERE YEARWEEK( FROM_UNIXTIME( `time`, '%Y-%m-%d %H:%i:%s' ) ,1) = YEARWEEK( now( ),1 ) //查询本月: $start = date('Y-m-01 00:00:00');
$end = date('Y-m-d H:i:s');
SELECT * FROM `table_name` WHERE `time` >= unix_timestamp('".$start."') AND `time` <= unix_timestamp('$end') //查询本年: $start = date('Y-01-01 00:00:00');
$end = date('Y-m-d H:i:s');
SELECT * FROM `table_name` WHERE `time` >= unix_timestamp( '$start' ) AND `time` <= unix_timestamp( '$end' )
php 获取今日、昨日、上周、本月的起始时间戳和结束时间
<?php
//<!--php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime()。--> //1、php获取今日开始时间戳和结束时间戳 $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1; echo $beginToday.'---'.$endToday;
echo '<br/>';
//2、php获取昨日起始时间戳和结束时间戳 $beginYesterday = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday = mktime(0,0,0,date('m'),date('d'),date('Y'))-1; echo $beginYesterday.'---'.$endYesterday;
echo '<br/>';
//3、php获取上周起始时间戳和结束时间戳 $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')); echo $beginLastweek.'---'.$endLastweek;
echo '<br/>'; //4、php获取本月起始时间戳和结束时间戳 $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y')); echo $beginThismonth.'---'.$endThismonth;
echo '<br/>'; //PHP mktime() 函数用于返回一个日期的 Unix 时间戳。
//语法:mktime(hour,minute,second,month,day,year,is_dst)
//
//参数 描述
//hour 可选。规定小时。
//minute 可选。规定分钟。
//second 可选。规定秒。
//month 可选。规定用数字表示的月。
//day 可选。规定天。
//year 可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。
//is_dst可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。
//自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。
//
//参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。 echo(date("M-d-Y",mktime(0,0,0,12,36,2001))); //将输出结果如:
//
//Jan-05-2002
自己实践
<?php
$conn = mysqli_connect("localhost","root","spirit","honeypot") or die("error in connection");
// total
$query = mysqli_query($conn, "SELECT count(*) FROM awshoneypot");
$count = mysqli_fetch_array($query);
echo number_format($count[0],0,".",",");
echo "</br>";
// daily
$beginToday = date('Y-m-d 00:00:00');
$endToday = date('Y-m-d 23:59:59');
$query1 = mysqli_query($conn, "SELECT count(*) FROM awshoneypot WHERE datetime >= '$beginToday' AND datetime <= '$endToday' ");
$count1 = mysqli_fetch_array($query1);
echo number_format($count1[0],0,".",",");
echo "</br>";
// weekly
$beginWeek = date("Y-m-d 00:00:00",mktime(0,0,0,date('m'),date('d')-date('w')+1-3,date('Y')));
$endWeek = date('Y-m-d 23:59:59');
$query2 = mysqli_query($conn, "SELECT count(*) FROM awshoneypot WHERE datetime >= '$beginWeek' AND datetime <= '$endWeek' ");
$count2 = mysqli_fetch_array($query2);
echo number_format($count2[0],0,".",",");
echo "</br>";
// monthly
$beginMonth=date("Y-m-d 00:00:00", mktime(0,0,0,date('m'),1,date('Y')));
$endMonth= date('Y-m-d 23:59:59');
$query3 = mysqli_query($conn, "SELECT count(*) FROM awshoneypot WHERE datetime >= '$beginMonth' AND datetime <= '$endMonth' ");
$count3 = mysqli_fetch_array($query3);
echo number_format($count3[0],0,".",",");
echo "</br>";
?>
php、mysql查询当天,查询本周,查询本月的数据实例(字段是时间戳)的更多相关文章
- Sql查询今天、本周和本月的记录(时间字段为时间戳)
工作中遇到的问题,小结一下 查询今日添加的记录: select * from [表名] where datediff(day,CONVERT(VARCHAR(20),DATEADD(SECOND,[时 ...
- mysql 查询当天、本周,本月,上一个月的数据---https://www.cnblogs.com/benefitworld/p/5832897.html
mysql 查询当天.本周,本月,上一个月的数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM ...
- SQL DATEDIFF语法及时间函数 Sql 查询当天、本周、本月记录
SQL DATEDIFF语法及时间函数 Sql 查询当天.本周.本月记录 转:http://blog.csdn.net/Json1204/article/details/7863801?locatio ...
- MySQL 查询当天、本周,本月、上一个月的数据
mysql查询当天的所有信息: SELECT * FROM 表名 WHERE year(时间字段名)=year(now()) and month(时间字段名) = month(now()) and d ...
- thinkphp 查询当天 ,本周,本月,本季度,本年度,全部, 数据方法
数据库字段是createtime 里面保存的是时间戳 <?php /* *按今天,本周,本月,本季度,本年,全部查询预约单数据 * $day 代表查询条件 $cid 代表 公司id *返回arr ...
- Sql 查询当天、本周、本月记录、上周、上月记录
查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 查询24小时内: select * from info where D ...
- Sql 查询当天、本周、本月记录
--查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...
- SQL查询当天、本周、本月记录详解
--查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...
- mysql 查询当天、本周,本月,上一个月的数据
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 近7天 DAY) <= date(时间字段名) 近30天 DAY) & ...
随机推荐
- R-时空可视化
Robert J. Hijmans37 开发了 raster 包用于网格空间数据的读.写.操作.分析和建模,同时维护了空间数据分析的网站 https://www.rspatial.org Edzer ...
- IronPython
当时做FitnesseTest的时候,写了很多和硬件交互的代码,但是后来发现每次都通过启动进程的方式运行python脚本,很费时间. 既然要运行python脚本,在.net平台下可以用IronPyth ...
- orchard 中文文档 中英对照版
ORCHARD CMS a free, open source, community-focused Content Management System built on the ASP.NET MV ...
- 代码问题【TADT//CVPR2019】
paper:Li X, Ma C, Wu B, et al. Target-Aware Deep Tracking[C]. //CVPR2019 调用adam.m时候报错 结构体内容引用自非结构体数组 ...
- AutoDesk公司搞的fbx模型格式
FBX® data exchange technology is a 3D asset exchange format that facilitates higher-fidelity data ex ...
- 设置 VMware 中的 Mac OS 虚拟机进入 Recovery 模式
Ø 简介 本文主要介绍 VMware 中的 Mac OS 虚拟机如何进入 Recovery 模式的方法,具体步骤如下: 1. 选择 Mac OS 虚拟机,点击"打开电源是进入固件&qu ...
- Autoware 1.12 安装/DEMO
前言 昨天试了一下新版本,发现完全按照官网安装会提示一些问题,所以留下记录. PS,我选择从源码安装Autoware 1.12 配置列表: 系统:Ubuntu 18.04 ROS:Melodic CU ...
- C# 中一个限制 Task 并发执行的数量的示例
直接贴代码了: using System; using System.Linq; using System.Threading; using System.Threading.Tasks; class ...
- 微软开放了.NET 4.5.1的源代码【转】
.NET Reference Source发布了beta版,可以在线浏览.NET Framework 4.5.1的源代码,并且可以通过配置,在Visual Studio 2013中调试.NET Fra ...
- 【05】Nginx:TCP / 正向 / 反向代理 / 负载均衡
写在前面的话 在我们日常的工作中,不可能所有的服务都是简单的 HTML 静态网页,nginx 作为轻量级的 WEB 服务器,其实我们将它用于更多的地方还是作为我们网站的入口.不管你是后端接口,还是前端 ...