php - Removing DateTime/Object from DatePeriod -
i'm trying create filter, whereby if days (monday, tuesday etc) not found in list, want specific datetime removed dateperiod. can say, work monday , tuesday. if find day thursday, continue out of loop , don't include it.
however, cannot seem when iterate through dateperiod, cannot unset anything, not count array. there way this? code can found below:
//get start , end dates of holidays (this run iteratively) $get_st_date = $row['h_s_date']; $get_end_date = $row['h_e_date']; //convert them approprariate format used dateperiod $get_begin = new datetime( "$get_st_date" ); $get_end = new datetime( "$get_end_date"); //add day or else omitted following process. $get_end = $get_end->add(new dateinterval('p1d')); //count per day $get_interval = dateinterval::createfromdatestring('1 day'); $get_period = new dateperiod($get_begin, $get_interval, $get_end); //iteration count $iter = 0; foreach($get_period $get_dt){ //find if date saturday or sunday. if is, break current loop. $iter++; $str_result = $get_dt->format('l'); if($str_result == "saturday") {continue;} elseif($str_result == "sunday") {continue;} elseif(!preg_match("($str_result)", $e_d_w_p_w)){ echo "<br>don't count day" . $str_result; unset($get_period[$iter]); continue; }
then close end tags (i haven't included here other stuff.
from above code, following error: "fatal error: uncaught error: cannot use object of type dateperiod array"
is there workaround this?
for clarification: $e_d_w_p_w "employee days worked per week"
$e_d_w_p_w formatted "monday;tuesday;" etc
the problem dateperiod
not array, error says. has properties required make list of days required, doesn't store them, can't unset()
specific day list.
what can accomplish create new array, , instead of removing days not match criteria dateperiod
, add days new array:
<?php $get_st_date = "2017-09-01"; $get_end_date = "2017-09-20"; //convert them approprariate format used dateperiod $get_begin = new datetime( "$get_st_date" ); $get_end = new datetime( "$get_end_date"); //add day or else omitted following process. $get_end = $get_end->add(new dateinterval('p1d')); //count per day $get_interval = dateinterval::createfromdatestring('1 day'); $get_period = new dateperiod($get_begin, $get_interval, $get_end); $e_d_w_p_w = "monday;tuesday;"; $workdays = []; //iteration count $iter = 0; foreach($get_period $get_dt) { //find if date saturday or sunday. if is, break current loop. $iter++; $str_result = $get_dt->format('l'); if($str_result == "saturday") {continue;} elseif($str_result == "sunday") {continue;} elseif(preg_match("($str_result)", $e_d_w_p_w)){ $workdays[] = $get_dt; } } var_dump($workdays);
also, think might bit cleaner (and faster; avoid regular expressions whenever possible) transform $e_d_w_p_w
array , check if current day in array:
$e_d_w_p_w = "monday;tuesday;"; $days = explode(";", $e_d_w_p_w); // transform array, separating ; array_pop($days); // remove last element (assuming have trailing ;
and then
elseif(in_array($str_result, $days)){ $workdays[] = $get_dt; }
Comments
Post a Comment