사이트 내 전체검색
PHP
1주 일요일, 3주 수요일 휴일 일때 날짜 추출 하는 방법. (정규식+php)
로빈아빠
https://cmd.kr/php/907 URL이 복사되었습니다.

본문

1. 주차,요일 배열 뽑기


$g5['tmp_arr_week1']=explode(' ','일 월 화 수 목 금 토');
$g5['tmp_arr_week2']=array_flip($g5['tmp_arr_week1']);

function tmp_get_off_date($buff) {
global $g5;
$buff=str_replace(', ',',',$buff);
$buff=str_replace(', ',',',$buff);
$buff=str_replace(',',', ',$buff);
//echo "\n$buff ->\n";
$arr_date=array();
if (preg_match("/([1-4])주, ([1-4])주 (.*?)요일/s",$buff,$match)) {
$arr_date[]="$match[1],".$g5['tmp_arr_week2'][$match[3]];
$arr_date[]="$match[2],".$g5['tmp_arr_week2'][$match[3]];
}
else if (preg_match("/([1-4])주(.*?)요일, ([1-4])주 (.*?)요일/s",$buff,$match)) {
$arr_date[]="$match[1],".$g5['tmp_arr_week2'][trim($match[2])];
$arr_date[]="$match[3],".$g5['tmp_arr_week2'][trim($match[4])];
}
return $arr_date;
}


function tmp_get_off_date_str($arr_date) {
    $t1=strtotime(date("Y-m-01"));
    $t2=strtotime(date("Y-m-t",strtotime("+1month",$t1)));
    $j=1;//주차
    $off_str='';
    for($t=$t1;$t<=$t2;$t+=24*3600){
$day=date("j",$t);
        $w=date("w",$t);
        if($w==0 && $day!=1) $j++;
else if ($j>1 && $day==1) $j=1;

if (in_array("{$j},{$w}",$arr_date)) {
if ($off_str) $off_str.=", ";
$off_str.=date("Y-m-d",$t);
}
    }
    return $off_str;
}


$arr_date=tmp_get_off_date($휴무일);
$off_date=tmp_get_off_date_str($arr_date);

결과
2주, 4주 일요일 -> 2018-03-04, 2018-03-18
2주 수요일, 4주 일요일 -> 2018-03-07, 2018-03-18

출처: howcode

댓글목록

등록된 댓글이 없습니다.

PHP
871 (1/18P)

Search

Copyright © Cmd 명령어 3.144.96.93