PHP生成带有月份和年份显示的数组

发布于 2024-12-08 08:03:45 字数 485 浏览 0 评论 0原文

我有两个表格的日期:

开始日期:2010年12月24日
结束日期:2011-10-06(今天)

现在我需要根据上面的开始日期和结束日期按月为该组生成一个数组:

$arr_month['oct_11'] = array(
   'month'   => 'October 2011'
 );
 . 
 . 
 .
$arr_month['feb_11'] = array(
   'month'   => 'Feb 2011'
 );

$arr_month['jan_11'] = array(
   'month'   => 'January 2011'
 );

$arr_month['dec_10'] = array(
   'month'   => 'December 2010'
 );

如您所见,如果我使用此表,表中应该有 11 行数组来生成表格。

I have two dates of the form:

Start Date: 2010-12-24
End Date: 2011-10-06 (today)

Now I need to generate an array for this group by month from the above start date and end date:

$arr_month['oct_11'] = array(
   'month'   => 'October 2011'
 );
 . 
 . 
 .
$arr_month['feb_11'] = array(
   'month'   => 'Feb 2011'
 );

$arr_month['jan_11'] = array(
   'month'   => 'January 2011'
 );

$arr_month['dec_10'] = array(
   'month'   => 'December 2010'
 );

As you can see the table should be got 11 rows in the table if I use this array to generate a table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风起 2024-12-15 08:03:45
$start = "2010-12-24";
$end = "2011-10-06";

function enumerate($start,$end) {
        $start = strtotime($start);
        $end = strtotime($end . "+1 month");
        $range = range($start,$end,60*60*24*31);
        $formatted = array();
        foreach($range as $date) {
        $key = strtolower(str_replace(" ","_",date("M Y",$date)));
        $formatted[$key] = array(

                        "month" => date('M Y',$date),
                        "amount" => "365"
                );
        }       
        return $formatted;
}

var_dump(enumerate($start,$end));
$start = "2010-12-24";
$end = "2011-10-06";

function enumerate($start,$end) {
        $start = strtotime($start);
        $end = strtotime($end . "+1 month");
        $range = range($start,$end,60*60*24*31);
        $formatted = array();
        foreach($range as $date) {
        $key = strtolower(str_replace(" ","_",date("M Y",$date)));
        $formatted[$key] = array(

                        "month" => date('M Y',$date),
                        "amount" => "365"
                );
        }       
        return $formatted;
}

var_dump(enumerate($start,$end));
末骤雨初歇 2024-12-15 08:03:45
$start_date = '2010-12-24';
$end_date = '2011-10-06';

function getMonthArray($start_date, $end_date){
    $start_timestamp = strtotime(str_replace('-', '/', $start_date));
    $end_timestamp = strtotime(str_replace('-', '/', $end_date));

    $start_month_timestamp = strtotime(date('F Y', $start_timestamp));
    $current_month_timestamp = strtotime(date('F Y', $end_timestamp));

    $arr_month = array();

    while( $current_month_timestamp >= $start_month_timestamp ) {
        $arr_month[strtolower(date('M_y', $end_timestamp))] = date('F Y', $end_timestamp);
        $end_timestamp = strtotime('-1 month', $end_timestamp);

        $current_month_timestamp = strtotime(date('F Y', $end_timestamp));
    }

    return $arr_month;
}

$arr_month = getMonthArray($start_date, $end_date);

演示

$start_date = '2010-12-24';
$end_date = '2011-10-06';

function getMonthArray($start_date, $end_date){
    $start_timestamp = strtotime(str_replace('-', '/', $start_date));
    $end_timestamp = strtotime(str_replace('-', '/', $end_date));

    $start_month_timestamp = strtotime(date('F Y', $start_timestamp));
    $current_month_timestamp = strtotime(date('F Y', $end_timestamp));

    $arr_month = array();

    while( $current_month_timestamp >= $start_month_timestamp ) {
        $arr_month[strtolower(date('M_y', $end_timestamp))] = date('F Y', $end_timestamp);
        $end_timestamp = strtotime('-1 month', $end_timestamp);

        $current_month_timestamp = strtotime(date('F Y', $end_timestamp));
    }

    return $arr_month;
}

$arr_month = getMonthArray($start_date, $end_date);

Demo

北斗星光 2024-12-15 08:03:45
<?php

$start = strtotime(substr_replace('2010-12-24', '01', -2));
$end = strtotime('2011-10-06');

$time = $start;
$arr_month = array();
while ($time <= $end) {
    $arr_month[strtolower(date('M_y', $time))]['month'] = date('F Y', $time);
    $time += date('t', $time) * 24 * 60 * 60;
}

var_dump($arr_month);

http://ideone.com/ozLOx

<?php

$start = strtotime(substr_replace('2010-12-24', '01', -2));
$end = strtotime('2011-10-06');

$time = $start;
$arr_month = array();
while ($time <= $end) {
    $arr_month[strtolower(date('M_y', $time))]['month'] = date('F Y', $time);
    $time += date('t', $time) * 24 * 60 * 60;
}

var_dump($arr_month);

http://ideone.com/ozLOx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文