如何将日期名称转换为当前月份的日期?

时间:2019-11-29 11:21:22

标签: php date time type-conversion dayofweek

我一个月中有几天的日期(星期五和星期天除外),因为它们没有加入政策,我想将日期名称数组转换为当前月份的日期,但是当我转换时,日期格式不正确因为它显示了不同月份enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式获取所需日期的数组:

//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);

您可以在此处运行此代码段:http://sandbox.onlinephpfunctions.com/code/8459152d9020d767110ad2732997af10d2e0d275