PHP或mysql如何提取前一月某日到后一月某日的记录

PHP或mysql如何提取前一月某日到后一月某日的记录,第1张

数据库的表里面专门加一列记录时间的,这样用SELECT查询的时候可以对时间进行限制了,根据你的情况就是

$time1 = mktime(201058);

$time2 = mktime(201048);

$query = "SELECT FROM WHERE time < time1 AND time >= time2";

至于mktime的参数怎么填,自己去查查手册吧。

还有,记得插入记录的时候,要把time列的值设成当前时间值,也就是$now=time();

你要实现的是不是当前月份和当前月份往前5个月,每个月的第一天是几号号最后一天是几号?如果是的话,我写了一个 能实现你的需求。你的问题让我好纠结。

$currentTime = time();

$cyear = floor(date("Y",$currentTime));

$cMonth = floor(date("m",$currentTime));

 

for($i=0;$i<6;$i++){

$nMonth = $cMonth-$i;

$cyear = $nMonth == 0  ($cyear-1) : $cyear;

$nMonth = $nMonth <= 0  12+$nMonth : $nMonth;

$date = $cyear"-"$nMonth"-1";

$firstday = date('Y-m-01', strtotime($date));

    $lastday = date('Y-m-t', strtotime($date));

echo $cyear"年"$nMonth"月";

echo "第一天:"$firstday;

echo "最后一天:"$lastday,"<br>";

}

这个time()函数是将时间保存成时间戳格式,则要查当月数据,只要查当月第一天到当月最后一天的之间的数据即可。

假设这个用来判断的字段是date

sql语句

SELECT ………… WHERE………… `date` >= 本月第一天的time值 AND `date` < 下个月第一天的time值

所以这里就只要获取当月第一天以及下个月第一天的时间戳

具体如下:

<php

$cur = date('Y-m',time());//当天年月

$cur_y = date('Y',time());//当天年份

$cur_m = date('m',time());//当天月份

$cur_f = $cur '-1';//本月首日

$first = strtotime($cur_f);//时间戳最小值,本月第一天时间戳

//下月首日

if($cur_m>=12){

$cur_n = ($cur_y+1) '-1-1';

}else{

$cur_n = $cur_y '-' ($cur_m+1) '-1';

}

$last = strtotime($cur_n);//时间戳最大值,下个月第一天时间戳

>

再把$first 和 $last 放入sql语句里面就可以查询到数据了

/

获取指定月份的第一天开始和最后一天结束的时间戳

@param int $y 年份 $m 月份

@return array(本月开始时间,本月结束时间)

/

function mFristAndLast($y="",$m=""){

if($y=="") $y=date("Y");

if($m=="") $m=date("m");

$m=sprintf("%02d",intval($m));

$y=str_pad(intval($y),4,"0",STR_PAD_RIGHT);

$m>12||$m<1$m=1:$m=$m;

$firstday=strtotime($y$m"01000000");

$firstdaystr=date("Y-m-01",$firstday);

$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));

return array("firstday"=>$firstday,"lastday"=>$lastday);

}

以上就是关于PHP或mysql如何提取前一月某日到后一月某日的记录全部的内容,包括:PHP或mysql如何提取前一月某日到后一月某日的记录、php中如何获取最近六个月每个月的起始时间和结束时间、php中用time()函数存入时间,如何查询当月的数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10059635.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存