使用日期格式打印一个月的奇数日期(2018年2月10日,星期六)

时间:2018-03-28 11:22:14

标签: php arrays date time

我已尝试使用此代码,但即时获取当月的所有日期,但实际上我只需要当月的奇数日期,任何人都可以建议我可以做哪些更改?

<?php

        $list=array();
        $month = 03;
        $year = 2018;

        for($d=1; $d<=31; $d++)
        {
            $time=mktime(02, 1, 2018, $month, $d, $year);          
            if (date('m', $time) == $month) {
                $list[] = date('D-d-m-Y', $time);
            }
        }
        echo "<pre>";
        print_r($list);
        echo "</pre>";

        ?>

3 个答案:

答案 0 :(得分:2)

如果$ outputFormat为DateTime ,此函数将返回带格式化日期的数组或null个对象

function getMonthDays($year, $month, $odd = true, $outputFormat = null) {
    $start = new DateTime(sprintf('%d-%d-0%d', $year, $month, $odd ? 1 : 2));

    $list = [];
    while ($start->format('n') == $month) {
        if ($outputFormat) {
            $list[] = $start->format($outputFormat);
        } else {
            $list[] = clone $start;
        }
        $start->modify('+2 day');
    }

    return $list;
}

print_r( getMonthDays(2020, 2, true, 'D j, M Y') );

demo

答案 1 :(得分:0)

您只需将增量更改为2而不是++

显示奇数日

$list=array();
$month = 03;
$year = 2018;

$num_days = date('t', strtotime("$year-$month-01")) ;
for($d=1; $d<=$num_days; $d+=2) // '+=2' instead of '++'
{
    $time = mktime(02, 1, 2018, $month, $d, $year);
    $list[] = date('D-d-m-Y', $time);
}
print_r($list);

输出:

Array
(
    [0] => Thu-01-03-2018
    [1] => Sat-03-03-2018
    [2] => Mon-05-03-2018
    [3] => Wed-07-03-2018
    [4] => Fri-09-03-2018
    [5] => Sun-11-03-2018
    [6] => Tue-13-03-2018
    [7] => Thu-15-03-2018
    [8] => Sat-17-03-2018
    [9] => Mon-19-03-2018
    [10] => Wed-21-03-2018
    [11] => Fri-23-03-2018
    [12] => Sun-25-03-2018
    [13] => Tue-27-03-2018
    [14] => Thu-29-03-2018
    [15] => Sat-31-03-2018
)

显示偶数日

$list=array();
$month = 03;
$year = 2018;

$num_days = date('t', strtotime("$year-$month-01")) ;
for($d=2; $d<=$num_days; $d+=2) // start at $d=2
{
    $time = mktime(02, 1, 2018, $month, $d, $year);
    $list[] = date('D-d-m-Y', $time);
}
print_r($list);

输出:

Array
(
    [0] => Fri-02-03-2018
    [1] => Sun-04-03-2018
    [2] => Tue-06-03-2018
    [3] => Thu-08-03-2018
    [4] => Sat-10-03-2018
    [5] => Mon-12-03-2018
    [6] => Wed-14-03-2018
    [7] => Fri-16-03-2018
    [8] => Sun-18-03-2018
    [9] => Tue-20-03-2018
    [10] => Thu-22-03-2018
    [11] => Sat-24-03-2018
    [12] => Mon-26-03-2018
    [13] => Wed-28-03-2018
    [14] => Fri-30-03-2018
)

答案 2 :(得分:0)

以下代码段将生成一个数组$oddDays,其中包含所选月份中的所有奇数天。

function getDateList($year, $month, $type = 'odd')
{
    $now = new DateTime();
    if (!$year) {
        $year = $now->format('Y');
    }

    if (!$month) {
        $month = $now->format('m');
    }

    // Let's start with the month you want the days from
    $startDate = DateTime::createFromFormat('Y-m-d', $year.'-'.$month.'-01');

    // Get the end date based on the start date
    $endDate = DateTime::createFromFormat('Y-m-d', $startDate->format('Y-m-t'));

    // The interval to increase with
    $interval = new DateInterval('P1D');

    // Define our date period
    $datePeriod = new DatePeriod($startDate, $interval, $endDate);

    // Define array and loop through dates
    $days = array();
    foreach ($datePeriod as $key => $date) {
        if ($type == 'odd') {
            if ($key % 2 == 0) {
                $days[] = $date->format('l d, ').strtoupper($date->format('M')).' '.$date->format('Y');
            }
        } elseif ($type == 'even') {
            if ($key % 2 != 0) {
                $days[] = $date->format('l d, ').strtoupper($date->format('M')).' '.$date->format('Y');
            }
        }
    }

    return $days;
}

echo "<pre>";

// Odd days
var_dump(getDateList('2018', '03', 'odd'));

// Even days
var_dump(getDateList('2018', '03', 'even'));

echo "</pre>";

输出

array(15) {
  [0]=>
  string(22) "Wednesday 01, AUG 2018"
  [1]=>
  string(19) "Friday 03, AUG 2018"
  [2]=>
  string(19) "Sunday 05, AUG 2018"
  [3]=>
  string(20) "Tuesday 07, AUG 2018"
  [4]=>
  string(21) "Thursday 09, AUG 2018"
  [5]=>
  string(21) "Saturday 11, AUG 2018"
  [6]=>
  string(19) "Monday 13, AUG 2018"
  [7]=>
  string(22) "Wednesday 15, AUG 2018"
  [8]=>
  string(19) "Friday 17, AUG 2018"
  [9]=>
  string(19) "Sunday 19, AUG 2018"
  [10]=>
  string(20) "Tuesday 21, AUG 2018"
  [11]=>
  string(21) "Thursday 23, AUG 2018"
  [12]=>
  string(21) "Saturday 25, AUG 2018"
  [13]=>
  string(19) "Monday 27, AUG 2018"
  [14]=>
  string(22) "Wednesday 29, AUG 2018"
}