함수 date_array()


1 개요[ | ]

함수 date_array()
날짜 목록 생성 함수 date_array()

2 Bash[ | ]

START=2019-12-30
END=2020-01-02
S=$(echo $START $END | awk '{
  split($1,s,"-")
  split($2,e,"-")
  s2=mktime(s[1] " " s[2] " " s[3] " 0 0 0")
  e2=mktime(e[1] " " e[2] " " e[3] " 0 0 0")
  for(i=s2;i<=e2;i+=86400)print strftime("%Y-%m-%d",i)
}')
ARR=(${S// / })
for VALUE in "${ARR[@]}"; do
        echo "[$VALUE]"
done
# [2019-12-30]
# [2019-12-31]
# [2020-01-01]
# [2020-01-02]

3 PHP[ | ]

function date_array($start, $end) {
	$reversed = ($start>$end);
	$min = $reversed ? $end : $start;
	$max = $reversed ? $start : $end;

	$dates = array();
	$date = $min;
	while( $date <= $max ) {
		$dates[] = $date;
		$date = date('Y-m-d', strtotime($date.' +1 day'));
	}
	return $reversed ? array_reverse($dates) : $dates;
}
print_r( date_array('1999-12-30', '2000-01-02') );
print_r( date_array('2000-01-02', '1999-12-30') );
/*
Array
(
    [0] => 1999-12-30
    [1] => 1999-12-31
    [2] => 2000-01-01
    [3] => 2000-01-02
)
Array
(
    [0] => 2000-01-02
    [1] => 2000-01-01
    [2] => 1999-12-31
    [3] => 1999-12-30
)
*/

4 같이 보기[ | ]

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