"함수 range2weeks()"의 두 판 사이의 차이

4번째 줄: 4번째 줄:
[[category: PHP]]
[[category: PHP]]
<source lang='PHP'>
<source lang='PHP'>
function range2weeks($range) {
function range2weeks($range, $interval='1 week') {
$start_date = $range[0].'-1';
$start_date = $range[0].'-1';
$end_date = $range[1].'-1';
$end_date = $range[1].'-1';
12번째 줄: 12번째 줄:
while( $date < $end_date ) {
while( $date < $end_date ) {
$weeks[] = date('o-\WW', strtotime($date));
$weeks[] = date('o-\WW', strtotime($date));
$date = date('o-\WW-N', strtotime("$date + 1 week"));
$date = date('o-\WW-N', strtotime("$date $interval"));
}
}
return $weeks;
return $weeks;
19번째 줄: 19번째 줄:
$weeks = range2weeks( array('2014-W50', '2015-W02') );
$weeks = range2weeks( array('2014-W50', '2015-W02') );
print_r( $weeks );
print_r( $weeks );
# Array ( [0] => 2014-W50 [1] => 2014-W51 [2] => 2014-W52 [3] => 2015-W01 )  
# Array ( [0] => 2014-W50 [1] => 2014-W51 [2] => 2014-W52 [3] => 2015-W01 )


$weeks = range2weeks( array('2015-W02', '2014-W50') );
$weeks = range2weeks( array('2015-W02', '2014-W50') );
print_r( $weeks );
print_r( $weeks );
# Array ( )
# Array ( )
$weeks = range2weeks( array('2014-W50', '2015-W09'), '3 weeks' );
print_r( $weeks );
# Array ( [0] => 2014-W50 [1] => 2015-W01 [2] => 2015-W04 [3] => 2015-W07 )
</source>
</source>


==같이 보기==
==같이 보기==
*[[함수 range()]]
*[[함수 range()]]

2015년 6월 24일 (수) 03:20 판


1 PHP

function range2weeks($range, $interval='1 week') {
	$start_date = $range[0].'-1';
	$end_date = $range[1].'-1';

	$weeks = array();
	$date = $start_date;
	while( $date < $end_date ) {
		$weeks[] = date('o-\WW', strtotime($date));
		$date = date('o-\WW-N', strtotime("$date $interval"));
	}
	return $weeks;
}

$weeks = range2weeks( array('2014-W50', '2015-W02') );
print_r( $weeks );
# Array ( [0] => 2014-W50 [1] => 2014-W51 [2] => 2014-W52 [3] => 2015-W01 )

$weeks = range2weeks( array('2015-W02', '2014-W50') );
print_r( $weeks );
# Array ( )

$weeks = range2weeks( array('2014-W50', '2015-W09'), '3 weeks' );
print_r( $weeks );
# Array ( [0] => 2014-W50 [1] => 2015-W01 [2] => 2015-W04 [3] => 2015-W07 )

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}