PHP时间函数-获取指定日期上个月的第一天和最后一天

/**
 * by hkshadow
 * Enter description here ...
 * @param 1 or 30 $i
 */
function GetPurMonth($date) { //获取指定日期上个月的第一天和最后一天
	$time = strtotime ( $date );
	$firstday = date ( 'Y-m-01', strtotime ( date ( 'Y', $time ) . '-' . (date ( 'm', $time ) - 1) . '-01' ) );
	$lastday = date ( 'Y-m-d', strtotime ( "$firstday +1 month -1 day" ) );
	return array ($firstday, $lastday );
}

/**
 * by hkshadow
 * Enter description here ...
 * @param 1 or 30 $i
 */
function GetNextMonth($date) { //获取指定日期下个月的第一天和最后一天
	$arr = getdate ();
	if ($arr ['mon'] == 12) {
		$year = $arr ['year'] + 1;
		$month = $arr ['mon'] - 11;
		$day = $arr ['mday'];
		if ($day < 10) {
			$mday = '0' . $day;
		} else {
			$mday = $day;
		}
		$firstday = $year . '-0' . $month . '-01';
		$lastday = $year . '-0' . $month . '-' . $mday;
	} else {
		$time = strtotime ( $date );
		$firstday = date ( 'Y-m-01', strtotime ( date ( 'Y', $time ) . '-' . (date ( 'm', $time ) + 1) . '-01' ) );
		$lastday = date ( 'Y-m-d', strtotime ( "$firstday +1 month -1 day" ) );
	}
	return array ($firstday, $lastday );
}

Leave a Reply

(will not be published)