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查询当天,查询本周,查询本月的数据实例(字段是时间戳)的更多相关文章

  1. Sql查询今天、本周和本月的记录(时间字段为时间戳)

    工作中遇到的问题,小结一下 查询今日添加的记录: select * from [表名] where datediff(day,CONVERT(VARCHAR(20),DATEADD(SECOND,[时 ...

  2. mysql 查询当天、本周,本月,上一个月的数据---https://www.cnblogs.com/benefitworld/p/5832897.html

    mysql 查询当天.本周,本月,上一个月的数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM ...

  3. SQL DATEDIFF语法及时间函数 Sql 查询当天、本周、本月记录

    SQL DATEDIFF语法及时间函数 Sql 查询当天.本周.本月记录 转:http://blog.csdn.net/Json1204/article/details/7863801?locatio ...

  4. MySQL 查询当天、本周,本月、上一个月的数据

    mysql查询当天的所有信息: SELECT * FROM 表名 WHERE year(时间字段名)=year(now()) and month(时间字段名) = month(now()) and d ...

  5. thinkphp 查询当天 ,本周,本月,本季度,本年度,全部, 数据方法

    数据库字段是createtime 里面保存的是时间戳 <?php /* *按今天,本周,本月,本季度,本年,全部查询预约单数据 * $day 代表查询条件 $cid 代表 公司id *返回arr ...

  6. Sql 查询当天、本周、本月记录、上周、上月记录

    查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 查询24小时内: select * from info where D ...

  7. Sql 查询当天、本周、本月记录

    --查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...

  8. SQL查询当天、本周、本月记录详解

    --查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...

  9. mysql 查询当天、本周,本月,上一个月的数据

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 近7天 DAY) <= date(时间字段名) 近30天 DAY) & ...

随机推荐

  1. 访问https问题

    访问https问题 package com.yuantiao.smartcardms.tools; import com.alibaba.fastjson.JSONObject; import com ...

  2. 开发JQuery插件标准结构

    1. 定义作用域 定义一个JQuery插件,首先要把这个插件的代码放在一个不受外界干扰的地方.如果用专业些的话来说就是要为这个插件定义私有作用域.外部的代码不能直接访问插件内部的代码.插件内部的代码不 ...

  3. Django CSRF

    CSRF(Cross-site request forgery)跨站请求伪造 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewM ...

  4. T-SQL(SQLSERVER)

    使用自定义类型名 CREATE DATABASE Student GO USE Student GO Exec sp_addtype char20,'varchar(20)','null' GO 在库 ...

  5. 逐行剖析Vue源码(一)——写在最前面

    1. 前言 博主作为一名前端开发,日常开发的技术栈是Vue,并且用Vue开发也有一年多了,对其用法也较为熟练了,但是对各种用法和各种api使用都是只知其然而不知其所以然,因此,有时候在排查bug的时候 ...

  6. 当usbnet打印 kevent * may have been dropped(转)

    http://patchwork.ozlabs.org/patch/815639/ Every once in a while when my system is under a bit of str ...

  7. LeetCode 141:环形链表 Linked List Cycle

    给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Given a l ...

  8. HDU 6148 (数位DP)

    ### HDU 6148 题目链接 ### 题目大意: 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Number,像山谷一样的数字. 当一个数字,从左到右依次看过去数字没有 ...

  9. SQLyog 图形化数据库的操作教程

    首先SQLyog作为mysql的图形化操作工具,是一款非常好用的工具. 操作说明 1.打开工具,点击[新建]输入名称.用户名:root,密码是安装时自己设置的(一定要记住的),端口号默认是:3306, ...

  10. C# SmtpClient 发邮件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...