0

次の 2 つの YYYYMM 文字列があるとします。

$start = "201301";
$end = "201303";

次のような配列を導出したいと思います。

array (
  0 => 
  array (
    0 => '20130101',
    1 => '20130131',
  ),
  1 => 
  array (
    0 => '20130201',
    1 => '20130228',
  ),
  2 => 
  array (
    0 => '20130301',
    1 => '20130331',
  ),
)

date() と mktime() でいくつか試してみましたが、今のところまともなものはありません。

これを行う良い方法は何ですか?

4

7 に答える 7

2

DateTimeの場合:

$start = "201301";
$end = "201303";

$dateNiceStart = substr($start,0,4).'-'.substr($start,4).'-01';
$dateNiceEnd = substr($end,0,4).'-'.substr($end,4).'-';

// Days count in month
$endDays = date('t',strtotime($dateNiceEnd.'01'));
$dateNiceEnd .= $endDays;

$startObj = new DateTime($dateNiceStart);
$endObj = new DateTime($dateNiceEnd);

$temp = clone $startObj;

$arr = array();
// Adding first month
//Using first day and last day
$temp->modify( 'first day of this month' );
$start = $temp->format('Ymd');
$temp->modify( 'last day of this month' );
$end = $temp->format('Ymd');

$arr[] = array($start, $end);
do{
    // for next month
    $temp->modify( 'first day of next month' );
    $start = $temp->format('Ymd');
    $temp->modify( 'last day of this month' );
    $end = $temp->format('Ymd');

    $arr[] = array($start, $end);

    // This line edited to work properly in different years, thanks to @Adidi
    $interval = $endObj->diff($temp)->format('%y%m%d');
}
while ($interval!=0);

print_R($arr);

主なキーは、翌月/今月の使用の最初/最後の日です。

于 2013-04-22T10:03:05.677 に答える
1

http://www.php.net/manual/en/function.date.php

必要なのは、フォーマット文字列「t」です。

t指定した月の日数 28 ~ 31

于 2013-04-22T09:50:06.833 に答える
0

これらの機能を試すことができます:

function get_date_range ($start, $end) {

    $dates = array();

    $start = strtotime($start .'01');
    $end    = strtotime($end .'01');

    while ($start <= $end) {
        $dates [] = array(date('Ym01', $start), date('Ymt', $start));
        $start  = strtotime(add_month(date('Y-m-01', $start), 1));
    }

    return $dates;
}

function add_month($date_value, $months, $format = 'm/d/Y') {
    $date = new DateTime($date_value);
    $start_day = $date->format('j');

    $date->modify("+{$months} month");
    $end_day = $date->format('j');

    if ($start_day != $end_day)
        $date->modify('last day of last month');

    return $date->format($format);
}

$start = "201301";
$end    = "201303";

$dates = get_date_range($start, $end);

print_r($dates);

ここで、add_month関数は、1 月 31 日に 1 か月を追加する際の日付の問題を回避するのに役立ちます。

使用strtotimeすると、1月31日に1か月を追加すると3月になります。出力は次のようになります。

Array
(
    [0] => Array
        (
            [0] => 2013-01-01
            [1] => 2013-01-31
        )

    [1] => Array
        (
            [0] => 2013-02-01
            [1] => 2013-02-28
        )

    [2] => Array
        (
            [0] => 2013-03-01
            [1] => 2013-03-31
        )

)

これがお役に立てば幸いです:)

于 2013-04-22T10:08:43.343 に答える
0

次のように月単位で繰り返すことができます。

function date_range($start, $end)
{
    $start = strtotime("$start01");
    $end = strtotime("$end01");

    $res = array();

    while ($start < $end) {
        $next = strtotime("+1 month -1 day", $start);
        $res[] = array(
            date('Ymd', $start),
            date('Ymd', $next),
        );
        $start = $next;
    }

    return $res;
}
于 2013-04-22T09:43:35.060 に答える