获得某日期前后N月月头、月尾值的函数

浏览:1064 发布日期:2013/10/14 分类:技术分享
今天需要用到查询某日期前后$month月数月头、月尾值,就搞了个函数,分享给大家,抛砖引玉哈!//getmonth获得某日期前后$month月数月头、月尾值
//$date是某日期,$month是月数,$start_end是月头、月尾
function getmonth($date,$month,$start_end) {
        if(checkdate($date) AND is_numeric($month)){
            if($month>0){
                $date10=date('Y-m-01',strtotime("$date + ".$month." month"));
            }elseif($month<0){
                $date10=date('Y-m-01',strtotime("$date - ".abs($month)." month"));
            }else{
                $date10=date('Y-m-01',strtotime("$date"));
            }
            $date1[1]=$date10;
            $date1[2]=date('Y-m-d',strtotime("$date10 + 1 month -1 day"));
            if($start_end==1){
                $dates=$date10;
            }elseif($start_end==2){
                $dates=$date1[2];
            }else{
                $dates=$date1;
            }
        }else{}
        return $dates;
    }
//范例:要查询2013-10-15前3月的第一天。
echo getmonth('2013-10-15','-3',1);
//输出 2013-07-01
//要查询2013-10-15后2个月最后一天
echo getmonth('2013-10-15',2,2);
//输出 2013-12-31
//要查询2013-10-15后2个月月头、月尾日期
dump(getmonth('2013-10-15',2));
//输出 数组 2013-12-01  2013-12-31
最佳答案
评论( 相关
后面还有条评论,点击查看>>