天天看点

php获取一年或一月或一周的时间期间

// 获取一月或一年或一周的日期期间
function getMonth($type="m"){
	if($type == "m"){
		//当前日期
		$date = date("Y-m-d");
	    $firstday = date("Y-m-01",strtotime($date));
	    $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
	    return array($firstday,$lastday);

	}elseif($type="w"){
		//当前日期

		$sdefaultDate = date("Y-m-d");

		//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期

		$first=1;

		//获取当前周的第几天 周日是 0 周一到周六是 1 - 6

		$w=date('w',strtotime($sdefaultDate));

		//获取本周开始日期,如果$w是0,则表示周日,减去 6 天

		$week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days'));
		//本周结束日期

		$week_end=date('Y-m-d',strtotime("$week_start +6 days"));

		return array($week_start,$week_end);
	}else{
		$begin_this_year=date('Y-01-01 00:00:00');  
		$end_this_year=date('Y-12-31 23:59:59');
		return array($begin_this_year,$end_this_year);
	}
 }
           
/**
 * 时长格式化(让人可以看懂的格式)
 * 2019-03-11
 */
function datetimebetter($timestamp){
    $day = floor($timestamp/86400);
    $hour = floor(($timestamp-$day*86400)/3600);
    $minutes = ceil(($timestamp-$day*86400-$hour*3600)/60);
    if($day > 0){
        echo $day.'天'.$hour.'小时'.$minutes.'分钟';
    }elseif($hour > 0){
        echo $hour.'小时'.$minutes.'分钟';
    }else{
        echo $minutes.'分钟';
    }
}


datetimebetter(15975);