本篇文章主要介绍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 获取时间的方法
js实现获取时间和设置倒计时代码分享
jquery获取时间方法总结
以上就是php、mysql查询当天,查询本周,查询本月的数据实例详解的详细内容。