构建日期范围

发布于 2024-08-12 02:49:55 字数 221 浏览 9 评论 0原文

我的开始日期为 20090101,结束日期为 20091130,我正在尝试构建和排列其间的所有月份,如下所示:

<?php

/* ... */

$arrDates['Jan'] = 2009;
$arrDates['Feb'] = 2009;
$arrDates['Mar'] = 2009;

/* ... */

?>

我该如何执行此操作?

I have a start date of 20090101 and an end date of 20091130 and I'm trying to build and array of all the months in between, which would look like this:

<?php

/* ... */

$arrDates['Jan'] = 2009;
$arrDates['Feb'] = 2009;
$arrDates['Mar'] = 2009;

/* ... */

?>

How can I do this?

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

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

发布评论

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

评论(6

冬天旳寂寞 2024-08-19 02:49:55

我不完全理解你的数组结构。

但这也许有帮助:当使用 PHP 5.3 时,您可以使用如下代码来获取给定范围内所有月份的迭代器:

<?php
$db = new DateTime( '2009-01-01 00:00:00' );
$de = new DateTime( '2009-11-30 23:59:59' );
$di = DateInterval::createFromDateString( 'first day of next month' );

foreach ( $di as $dt )
{
    echo $dt->format( "Y-m\n" );
}
?>

I don't fully understand your array structure.

But maybe this helps: When using PHP 5.3 you can use code like below to get an iterator with all months in the given range:

<?php
$db = new DateTime( '2009-01-01 00:00:00' );
$de = new DateTime( '2009-11-30 23:59:59' );
$di = DateInterval::createFromDateString( 'first day of next month' );

foreach ( $di as $dt )
{
    echo $dt->format( "Y-m\n" );
}
?>
伪心 2024-08-19 02:49:55

以下代码片段创建了这样一个数组:

$startDate = '20090101';
$endDate = '20091130';
$arrDates = array();

$cur = strtotime($startDate);
$end = strtotime($endDate);
while ($cur < $end) {
    $arrDates[date('M', $cur)] = date('Y', $cur);
    $cur = mktime(0, 0, 0, date('m', $cur) + 1, 1, date('Y', $cur));
}

// If you want to add the 'end' month too...
$arrDates[date('M', $end)] = date('Y', $end);

但是,请注意,正如 danii 所评论的那样,您不清楚如何处理大于一年的时间跨度。上面的代码将仅使用您提供的范围内的最后一年。

该代码几乎适用于任何版本的 PHP(PHP 4+)。如果您想要更优雅的解决方案并且使用 PHP 5.2+,我推荐 GZipp 提供的解决方案。

The following snippet creates such an array:

$startDate = '20090101';
$endDate = '20091130';
$arrDates = array();

$cur = strtotime($startDate);
$end = strtotime($endDate);
while ($cur < $end) {
    $arrDates[date('M', $cur)] = date('Y', $cur);
    $cur = mktime(0, 0, 0, date('m', $cur) + 1, 1, date('Y', $cur));
}

// If you want to add the 'end' month too...
$arrDates[date('M', $end)] = date('Y', $end);

However, note that, as danii commented, you are unclear about how you want to handle a timespan that is larger than a year. The code above will simply use the last year in the range you provide.

This code will work with pretty much any version of PHP (PHP 4+). If you want a more elegant solution and are using PHP 5.2+, I recommend the solution offered by GZipp.

万水千山粽是情ミ 2024-08-19 02:49:55

我为旅行社建立的网站也遇到了类似的情况。您需要时间戳、数组和循环。看看PHP提供的日期函数。它为您提供了一些有趣的日期选择。例如,没有。指定月份中的天数。

I had a similar situation for a website i was building for travelagency. You need timestamps, arrays and looping. Take a look at the date function PHP provides. It gives you some interesting options to play with dates. E.g. the no. of days in a specified month.

夏末 2024-08-19 02:49:55

您说“中间的月份”,但由于您的示例包括开始月份,我假设您的意思是“中间的月份加上开始和结束的月份”。

$dt_start = new DateTime('20090101');
$dt_end   = new DateTime('20091130');
$arrDates[] = $dt_start->format('M');
while ($dt_start->modify('first day of next month') <= $dt_end) {
    $arrDates[] = $dt_start->format('M');  // Or whatever you want to do with it.
}

(这本质上是 johannes 的解决方案,通过一些手动阅读来使其适应 PHP 5.2。)

You say "the months in between", but since your example includes the starting month, I assume you mean "the months in between plus the starting and ending months".

$dt_start = new DateTime('20090101');
$dt_end   = new DateTime('20091130');
$arrDates[] = $dt_start->format('M');
while ($dt_start->modify('first day of next month') <= $dt_end) {
    $arrDates[] = $dt_start->format('M');  // Or whatever you want to do with it.
}

(This is essentially johannes' solution with a little manual reading applied to adapt it for PHP 5.2.)

少年亿悲伤 2024-08-19 02:49:55

您不能使用月份作为键,键必须是唯一的,否则如果您的范围跨越一年以上,它将无法正常工作。
这将返回一个以 Mon-Year 为键的数组

function foo($startdate, $enddate) {

    // create a timestamp for start date
    if(!preg_match('/^(\d{4})(\d{2})(\d{2})$/', $startdate, $m)) die('Invalid start date format');
    $start_time = mktime(0, 0, 0, $m[2], $m[3], $m[1]);

    // create a timestamp for end date
    if(!preg_match('/^(\d{4})(\d{2})(\d{2})$/', $enddate, $m)) die('Invalid end date format');
    $end_time = mktime(23, 59, 59, $m[2], $m[3], $m[1]);

    // build the array of months by incrementing $start_time by one month through each iteration
    $ret = array();
    while($start_time < $end_time) {
        $ret[date('M-Y', $start_time)] = date('Y', $start_time);
        $start_time = strtotime(date('m/d/Y', $start_time).' +1month');
    }

    return $ret;
}

$arrDates = foo('20090101', '20111130');
print_r($arrDates);

Array(
    [Jan-2009] => 2009
    [Feb-2009] => 2009
    [Mar-2009] => 2009
    [Apr-2009] => 2009
    [May-2009] => 2009
    [Jun-2009] => 2009
    [Jul-2009] => 2009
    [Aug-2009] => 2009
    ....
)

You can't use the month as a key, the key must be unique or if your range spans more than a year it won't work correctly.
This will return an array with Mon-Year as the key

function foo($startdate, $enddate) {

    // create a timestamp for start date
    if(!preg_match('/^(\d{4})(\d{2})(\d{2})$/', $startdate, $m)) die('Invalid start date format');
    $start_time = mktime(0, 0, 0, $m[2], $m[3], $m[1]);

    // create a timestamp for end date
    if(!preg_match('/^(\d{4})(\d{2})(\d{2})$/', $enddate, $m)) die('Invalid end date format');
    $end_time = mktime(23, 59, 59, $m[2], $m[3], $m[1]);

    // build the array of months by incrementing $start_time by one month through each iteration
    $ret = array();
    while($start_time < $end_time) {
        $ret[date('M-Y', $start_time)] = date('Y', $start_time);
        $start_time = strtotime(date('m/d/Y', $start_time).' +1month');
    }

    return $ret;
}

$arrDates = foo('20090101', '20111130');
print_r($arrDates);

Array(
    [Jan-2009] => 2009
    [Feb-2009] => 2009
    [Mar-2009] => 2009
    [Apr-2009] => 2009
    [May-2009] => 2009
    [Jun-2009] => 2009
    [Jul-2009] => 2009
    [Aug-2009] => 2009
    ....
)
另类 2024-08-19 02:49:55

有点复杂但有效......:

function buildDateRange($strStartDate, $strEndDate) 
{                   

    $strStartM = date('M', $strStartDate);
    $strStartY = date('Y', $strStartDate);

    $strEndM = date('M', $strEndDate);
    $strEndY = date('Y', $strEndDate);

    $intCurMN = date('m', $strStartDate);       

    $ii = 0;
    $blnFinished = FALSE;

    while(!$blnFinished) 
    {

        $strCurM  = date('M', mktime(0, 0, 0, $intCurMN , "01", $strStartY));                   
        $strCurY  = date('Y' ,mktime(0, 0, 0, $intCurMN , "01", $strStartY));                                       

        $arrSearchDates [$strCurM] = $strCurY;

        $intCurMN = date('m', mktime(0, 0, 0, $intCurMN+1 , "01", $strStartY));                 


        if($strEndM == $strCurM && $strEndY == $strCurY) 
        {
            $blnFinished = TRUE;    
        }
    }

    Return ($arrSearchDates);

}

A bit convoluted but works...:

function buildDateRange($strStartDate, $strEndDate) 
{                   

    $strStartM = date('M', $strStartDate);
    $strStartY = date('Y', $strStartDate);

    $strEndM = date('M', $strEndDate);
    $strEndY = date('Y', $strEndDate);

    $intCurMN = date('m', $strStartDate);       

    $ii = 0;
    $blnFinished = FALSE;

    while(!$blnFinished) 
    {

        $strCurM  = date('M', mktime(0, 0, 0, $intCurMN , "01", $strStartY));                   
        $strCurY  = date('Y' ,mktime(0, 0, 0, $intCurMN , "01", $strStartY));                                       

        $arrSearchDates [$strCurM] = $strCurY;

        $intCurMN = date('m', mktime(0, 0, 0, $intCurMN+1 , "01", $strStartY));                 


        if($strEndM == $strCurM && $strEndY == $strCurY) 
        {
            $blnFinished = TRUE;    
        }
    }

    Return ($arrSearchDates);

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