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) & ...
随机推荐
- [C4W4] Convolutional Neural Networks - Special applications: Face recognition & Neural style transfer
第四周:Special applications: Face recognition & Neural style transfer 什么是人脸识别?(What is face recogni ...
- win10打印所有进程
#include <map> #include <iostream> #include <string> #include <windows.h> #i ...
- php 学习笔记之搭建开发环境(mac版)
Mac 系统默认集成了很多开发工具,其中就包括 php 所需要的一些软件工具. 下面我们将搭建最简单的 php 开发环境,每一步都会验证上一步的操作结构,请一步一步跟我一起搭建吧! web 服务器之 ...
- RabbitMQ的使用(五)RabbitMQ Java Client简单生产者、消费者代码示例
pom文件: <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artif ...
- ESP8266 LUA脚本语言开发: 外设篇-GPIO中断检测
https://nodemcu.readthedocs.io/en/master/modules/gpio/#gpiomode 测试引脚 GPIO0 gpio.mode(,gpio.INT) func ...
- 【BZOJ4556】[TJOI2016&HEOI2016] 字符串(后缀自动机+线段树合并+二分)
点此看题面 大致题意: 给你一个字符串\(s\),每次问你一个子串\(s[a..b]\)的所有子串和\(s[c..d]\)的最长公共前缀. 二分 首先我们可以发现一个简单性质,即要求最长公共前缀,则我 ...
- C++ TCP客户端网络消息发送接收同步实现
废话不多说, 直入主题, 我们在写客户单的时候希望在哪里发消息出去,然后在哪里返回消息(同步), 然后继续往下运行-, 而不是在这里发送了一个消息给服务端, 在另一个地方接受消息(异步) , 也不知道 ...
- LCT好题总结
写在前面: 初探多项式之后,开始了数据结构之旅,可持久化数据结构的总结大概是咕了,只总结一些$LCT$的题 T1:水管局长数据加强版 发现题中只有删边操作,而我们只会做加边,所有考虑时光倒流 先在最后 ...
- [开源]OSharpNS 步步为营系列 - 4. 添加业务对外API
什么是OSharp OSharpNS全称OSharp Framework with .NetStandard2.0,是一个基于.NetStandard2.0开发的一个.NetCore快速开发框架.这个 ...
- hdu-6071 Lazy Running
In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule ...