【发布时间】:2018-07-13 23:02:39
【问题描述】:
我正在尝试创建一个过滤器,如果在列表中找不到日期(星期一、星期二等),我希望从我的 DatePeriod 中删除特定的 DateTime。这样我就可以说,我周一和周二工作。如果您发现这一天是星期四,请继续退出此循环并且不要包含它。
但是,我似乎无法执行此操作,因为当我遍历 DatePeriod 时,我无法取消设置任何内容,因为它不会将其视为数组。有没有办法做到这一点?代码如下:
//Get all the start and end dates of the holidays (This will be run iteratively)
$get_st_date = $row['h_s_date'];
$get_end_date = $row['h_e_date'];
//Convert them to approprariate format to be used with DatePeriod
$get_begin = new DateTime( "$get_st_date" );
$get_end = new DateTime( "$get_end_date");
//Add an extra day or else it will be omitted from the 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 as $get_dt){
//Find if date is Saturday or Sunday. If it is, break that 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 this day" . $str_result;
unset($get_period[$iter]);
continue;
}
然后关闭结束标签(我没有在这里包含它,因为我做一些其他的事情。
从上面的代码中,我得到以下错误: “致命错误:未捕获的错误:不能使用 DatePeriod 类型的对象作为数组”
有解决办法吗?
澄清:$e_d_w_p_w 是“员工每周工作天数”
$e_d_w_p_w 的格式类似于“Monday;Tuesday;”等等
【问题讨论】:
-
$e_d_w_p_w是否类似于字符串中以逗号分隔的日期列表?你能举个例子吗? -
@ishegg 更新了问题,以提供关于 $e_d_w_p_w 的更好说明。 "$e_d_w_p_w 的格式类似于 "Monday;Tuesday" 等