【发布时间】:2020-03-24 23:53:37
【问题描述】:
我在一个月中有一系列天数,不包括周五和周日,因为它们没有包含在政策中,我想将天数名称数组转换为当月的日期,但是当我转换它时格式不正确因为它显示不同的月份enter image description here
【问题讨论】:
标签: php date time type-conversion dayofweek
我在一个月中有一系列天数,不包括周五和周日,因为它们没有包含在政策中,我想将天数名称数组转换为当月的日期,但是当我转换它时格式不正确因为它显示不同的月份enter image description here
【问题讨论】:
标签: php date time type-conversion dayofweek
您可以通过以下方式获取您想要的所有日期的数组:
//create a array of DateTimes of the days in the month
$allDateTime = iterator_to_array(new DatePeriod(new DateTime(date('Y-m') . '-1'), new DateInterval('P1D'), new DateTime(date('Y-m-t'))));
//filter out the Fridays and Sundays
$filteredDateTimes = array_filter($allDateTime, function ($date) {
$day = $date->format("N");
return $day !== '7' && $day !== '5'; //7 for sunday, 5 for friday
});
//format the to dd-mm-yyyy
$filteredDates = array_map(function ($date) {
return $date->format("d-m-Y"); //you can choose the format you prefer here
}, $filteredDateTimes);
print_r($filteredDates);
你可以在这里运行这个 sn-p:http://sandbox.onlinephpfunctions.com/code/8459152d9020d767110ad2732997af10d2e0d275
【讨论】: