拽拽
管理员组

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);
}

?>




#1楼
发帖时间:2015-5-25   |   查看数:0   |   回复数:0
游客组