php计算上月数据 php获取上月最后一天

php计算上个月是几月份?php
$d=getdate();
$thismonth=$d['mon'];
$lastmonth=$thismonth-1;
if($lastmonth==0)
{
$lastmonth=12;
}
echo("上个月是:"
?
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);
【php计算上月数据 php获取上月最后一天】}
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);
}
?
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;
如何用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');
其它获取类似以上的代码显示
如何使用PHP计算上一个月的今天?php
$time = time();
/**
* 计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天
* @param type $time
* @return type
*
*/
function last_month_today($time){
$last_month_time = mktime(date("G", $time), date("i", $time),

推荐阅读