0

次の配列を手動で作成しました (下)。簡単に展開できる 2 つの文字列 (explode()) のみを使用してプログラムで作成したいと考えています。

$min_date = '2012-03-01 00:00:00';
$max_date = '2012-04-25 00:00:00';

私はすでに文字列を爆発()させ、2つの日付の間に何ヶ月あるかを調べました

    $min_date = explode('-',$min_date);
    $min_date['year'] = intval($min_date[0]);
    $min_date['month'] = intval($min_date[1]);
    $min_date['day'] = intval($min_date[2]);

    $max_date = explode('-',$max_date);
    $max_date['year'] = intval($max_date[0]);
    $max_date['month'] = intval($max_date[1]);
    $max_date['day'] = intval($max_date[2]);

$months_between = ($max_date['month'] - $min_date['month']) + 12 * ($max_date['year'] - $min_date['year']);

1を返します

cal_days_in_month()関数も便利なようですが、方法がわかりません...

$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year)

これが私が手動で作成した配列です!

$periods = array(
  '0' => array(
    'month'=>'march',
    'week'=>'1',
    'days'=>'4',
    'readable'=>' Thurs Mar, 01, 2012 - Sun Mar, 04, 2012',
    'created_at_min'=>'2012-03-01 00:00:00',
    'created_at_max'=>'2012-03-04 24:00:00'
  ),
  '1' => array(
    'month'=>'march',
    'week'=>'2',
    'days'=>'7',
    'readable'=>' Mon Mar, 05, 2012 - Sun Mar, 11, 2012',
    'created_at_min'=>'2012-03-05 00:00:00',
    'created_at_max'=>'2012-03-11 24:00:00'
  ),
  '2' => array(
    'month'=>'march',
    'week'=>'3',
    'days'=>'7',
    'readable'=>' Mon Mar, 12, 2012 - Sun Mar, 18, 2012',
    'created_at_min'=>'2012-03-12 00:00:00',
    'created_at_max'=>'2012-03-18 24:00:00'
  ),
  '3' => array(
    'month'=>'march',
    'week'=>'4',
    'days'=>'7',
    'readable'=>' Mon Mar, 19, 2012 - Sun Mar, 25, 2012',
    'created_at_min'=>'2012-03-19 00:00:00',
    'created_at_max'=>'2012-03-25 24:00:00'
  ),
  '4' => array(
    'month'=>'march',
    'week'=>'5',
    'days'=>'6',
    'readable'=>' Mon Mar, 26, 2012 - Sun Mar, 31, 2012',
    'created_at_min'=>'2012-03-26 00:00:00',
    'created_at_max'=>'2012-03-31 24:00:00'
  ),
  '5' => array(
    'month'=>'april',
    'week'=>'1',
    'days'=>'1',
    'readable'=>' Sun Apr, 1, 2012',
    'created_at_min'=>'2012-04-01 00:00:00',
    'created_at_max'=>'2012-04-01 24:00:00'
  ),
  '6' => array(
    'month'=>'april',
    'week'=>'2',
    'days'=>'7',
    'readable'=>' Mon Apr, 2, 2012 - Sun Apr, 8, 2012',
    'created_at_min'=>'2012-04-01 00:00:00',
    'created_at_max'=>'2012-04-08 24:00:00'
  ),
  '7' => array(
    'month'=>'april',
    'week'=>'3',
    'days'=>'7',
    'readable'=>' Mon Apr, 9, 2012 - Sun Apr, 15, 2012',
    'created_at_min'=>'2012-04-09 00:00:00',
    'created_at_max'=>'2012-04-15 24:00:00'
  ),
  '8' => array(
    'month'=>'april',
    'week'=>'4',
    'days'=>'7',
    'readable'=>' Mon Apr, 16, 2012 - Sun Apr, 22, 2012',
    'created_at_min'=>'2012-04-16 00:00:00',
    'created_at_max'=>'2012-04-22 24:00:00'
  ),
  '9' => array(
    'month'=>'april',
    'week'=>'5',
    'days'=>'3',
    'readable'=>' Mon Apr, 23, 2012 - Wed Apr, 25, 2012',
    'created_at_min'=>'2012-04-16 00:00:00',
    'created_at_max'=>'2012-04-25 00:00:00' // note last day timestamp needs to be 00:00:00
  ),
);

これに飛び込む前に、PHPが提供するもので非常に役立つものはありますか?

$weeks = array(
  'may' = array(1,2,3,4,5),
  'april' = array(1,2,3,4,5),
);
4

1 に答える 1

1
$min_date = new DateTime('2012-03-01 00:00:00');
$max_date = new DateTime('2012-04-25 00:00:00');
$numOfWeeks = ceil($min_date->diff($max_date)->days / 7.0);

$baseDate = $min_date->getTimestamp();
$startOfWeek = 'Mon';
for ($i = 0; $i <= $numOfWeeks + 1; $i++)
{
    $weekRangeStart = date('D M, d, Y', $baseDate);
    if (date('M', strtotime("Next Sunday", $baseDate)) == date('M', $baseDate))
    {
        $weekRangeEnd = date('D M, d, Y', strtotime("Next Sunday", $baseDate));
        $nextDateObject = new DateTime(date('r', strtotime('Next Monday', $baseDate)));
    }
    else
    {
        $weekRangeEnd = date('D M, d, Y', strtotime("last day of this month", $baseDate));
        $nextDateObject = new DateTime(date('r', strtotime("last day of this month", $baseDate)));
    }

    $baseDateObject = new DateTime(date('r', $baseDate));

    $daysInWeek = $baseDateObject->diff($nextDateObject)->days;

    $periods[] = array('month' => date('F', $baseDate), 'days' => $daysInWeek, 
                'readable' => $weekRangeStart . ' - ' . $weekRangeEnd);

    if (date('D', $baseDate) != $startOfWeek)
        $baseDate = strtotime("Next Monday", $baseDate);
    else
        $baseDate = strtotime("+1 week", $baseDate);
}

var_dump($periods);

このようなことをしてください。次のような配列が得られます。

    array
  0 => 
    array
      'month' => string 'March' (length=5)
      'days' => int 4
      'readable' => string 'Thu Mar, 01, 2012 - Sun Mar, 04, 2012' (length=37)
  1 => 
    array
      'month' => string 'March' (length=5)
      'days' => int 7
      'readable' => string 'Mon Mar, 05, 2012 - Sun Mar, 11, 2012' (length=37)
  2 => 
    array
      'month' => string 'March' (length=5)
      'days' => int 7
      'readable' => string 'Mon Mar, 12, 2012 - Sun Mar, 18, 2012' (length=37)
  3 => 
    array
      'month' => string 'March' (length=5)
      'days' => int 7
      'readable' => string 'Mon Mar, 19, 2012 - Sun Mar, 25, 2012' (length=37)
  4 => 
    array
      'month' => string 'March' (length=5)
      'days' => int 5
      'readable' => string 'Mon Mar, 26, 2012 - Sat Mar, 31, 2012' (length=37)
  5 => 
    array
      'month' => string 'April' (length=5)
      'days' => int 7
      'readable' => string 'Mon Apr, 02, 2012 - Sun Apr, 08, 2012' (length=37)
  6 => 
    array
      'month' => string 'April' (length=5)
      'days' => int 7
      'readable' => string 'Mon Apr, 09, 2012 - Sun Apr, 15, 2012' (length=37)
  7 => 
    array
      'month' => string 'April' (length=5)
      'days' => int 7
      'readable' => string 'Mon Apr, 16, 2012 - Sun Apr, 22, 2012' (length=37)
  8 => 
    array
      'month' => string 'April' (length=5)
      'days' => int 7
      'readable' => string 'Mon Apr, 23, 2012 - Sun Apr, 29, 2012' (length=37)
  9 => 
    array
      'month' => string 'April' (length=5)
      'days' => int 0
      'readable' => string 'Mon Apr, 30, 2012 - Mon Apr, 30, 2012' (length=37)

編集: 週が月にまたがらないようにコードが更新されました。

于 2012-04-26T00:54:56.577 に答える