dd

php获取指定月的某一天的日期

jerry thinkphp 2015年11月18日 收藏
php获取指定月的某一天的日期
   /**
      * 获取指定月的第几天的日期
      * @$year 年,$month月,$day天,$format显示时间的格式,$last是否该月最后一天
      * @return $str
      */
function getMonthDay($year,$month,$day,$format='Y-m-d',$last=0){
        if($last){
            $time=new DateTime($day);        
            $d=strtotime($time->format("Y-m-d"));
            $d=date("t",$d);
            $time->setDate($year,$month,$d);
        }else{
            $time=new DateTime();
            $time->setDate($year,$month,$day);
        }
        return $time->format($format);
    }
如要获取2013年3月第一天,getMonthDay(2013,3,1)返回:2013-03-01
如要获取2013年最后一天,getMonthDay(2013,2,'',1)返回2013-02-28
dd