
正文
php计算上月数据 php获取上月最后一天
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
php计算上个月是几月份
?php
$d=getdate();
$thismonth=$d['mon'];
$lastmonth=$thismonth-1;
if($lastmonth==0)
{
$lastmonth=12;
}
echo("上个月是:"
?
相关问答
Q1: PHP如何获取上月月份时间,请高手指教记得附上完整代码哦,在下感激不尽
我觉得你应该是要上个月份的起始日期和结束日期吧?我前段时间也遇到这个问题的,希望下面的代码能够帮到你。
?php
$d = '2013-2-28';
print_r(premonth($d));
print_r(nextmonth($d));
/*
Jamers 2013.4.11
无奈,PHP5.2没有date_add,并且用strtotime('+1 month')的方式不符合我的要求
只能自己动手写了,取上个月/下个月的起始天和结束天
*/
function premonth($d) {
$t = strtotime($d);
$m = date('m',$t);
$t -= 28*24*60*60;
if ($m == date('m',$t)) {
$t -= 3*24*60*60;
}
$s = date('Y-m-d',$t);
$a[0] = fristday($s);
$a[1] = lastday($s);
return $a;
}
function nextmonth($d) {
$t = strtotime($d);
$t += 28*24*60*60;
$m = date('m',$t);
if ($m == date('m',strtotime($d))) $t += 3*24*60*60;
$s = date('Y-m-d',$t);
$a[0] = fristday($s);
$a[1] = lastday($s);
return $a;
}
function fristday($d) {
$t = strtotime($d);
return date('Y-m-01',$t);
}
function lastday($d) {
$t = strtotime($d);
$t += 28*24*60*60; //加上28天
$m = date('m',$t);
if ($m == date('m',strtotime($d))) $t += 3*24*60*60;
$t = strtotime(date("Y-m-01",$t));
$t -= 24*60*60;
return date('Y-m-d',$t);
}
?
Q2: php如何求上一个月月初至月末?
由于php内置时间函数 strtotime 在求上个月这个功能上存在bug,所以放弃不用了……
上个自己写的临时用的,楼主看看:
$thismonth = date('m');
$thisyear = date('Y');
if($thismonth==1) {
$lastmonth = 12;
$lastyear = $thisyear-1;
} else {
$lastmonth = $thismonth - 1;
$lastyear = $thisyear;
}
$lastStartDay = $lastyear.'-'.$lastmonth.'-1';
$lastEndDay = $lastyear.'-'.$lastmonth.'-'.date('t',strtotime($lastStartDay));
echo 'lastStartDay = '.$lastStartDay;
echo 'br/';
echo 'lastEndDay = '.$lastEndDay;
Q3: 如何用PHP 获取今天之前,本周之前,本月之前,本年之前,今天,本周,本月,本年的数据呢
/*今天*/
select * from 表名 where to_days(时间字段) = to_days(now());
/*昨天*/
select * from 表名 where to_days(now())-to_days(时间字段) = 1;
/*近7天*/
select * from 表名 where date_sub(curdate(), interval 7 day) = date(时间字段);
/*查询距离当前现在6个月的数据*/
select * from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();
/*查询当前这周的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now());
/*查询上周的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now())-1;
/*查询当前月份的数据*/
select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(now(),'%Y-%m');
/*查询上个月的数据*/
select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(date_sub(curdate(), interval 1 month),'%Y-%m');
其它获取类似以上的代码显示
Q4: 如何使用PHP计算上一个月的今天
?php
$time = time();
/**
* 计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天
* @param type $time
* @return type
*
*/
function last_month_today($time){
$last_month_time = mktime(date("G", $time), date("i", $time),
date("s", $time), date("n", $time), 0, date("Y", $time));
$last_month_t = date("t", $last_month_time); //二月份的天数
if ($last_month_t date("j", $time)) {
return date("Y-m-t H:i:s", $last_month_time);
}
return date(date("Y-m", $last_month_time) . "-d", $time);
}
echo last_month_today($time);
Q5: php怎么做1号至15号查询上个月的资料
这个查询的sql语句是这样的select * from table(你查询资料的数据表名) where addtime(你资料表中添加时间的字段)=(1号的时间戳) and addtime=(15号的时间戳),这样你看下你要求出几个变量php计算上月数据了php计算上月数据:1号的时间戳和15号的时间戳,这样就可以查询出 1号至15号的资料了
php计算上月数据的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php获取上月最后一天、php计算上月数据的信息别忘了在本站进行查找喔。






