Pergunta

I am trying to calculate monthly recurring events based on a calendar date range. However I stumbled upon a problem with February (28 days) calculation when an event falls on the 29th of the month. Hope some expert may give me some pointers as I tried searching around with no help.

I have included the following code for easy to run straight as a script. The January range is working fine which gives me 2 dates which falls on the range:

// JANUARY 2013 Range (Working)
2012-12-29
2013-01-29

but if you uncomment the February range, it starts to output:

2013-01-29 // This is correct
2013-02-28 // This is the date which I want to ignore because its not on the 29th.

And if you uncomment the April range, it starts to output nothing at all. It should output one date which is 2013-04-29. So therefore the problem lies with Feb calculation I suppose.

<?php

// Start Calendar Range

// JANUARY 2013 Range (Working)
$rangeStart = new DateTime( '2012-12-30' );
$rangeEnd   = new DateTime( '2013-02-03' );

// FEBRUARY 2013 Range (Not Working)
//$rangeStart = new DateTime( '2013-01-27' );
//$rangeEnd   = new DateTime( '2013-03-03' );

// APRIL 2013 Range (Not Working)
//$rangeStart = new DateTime( '2013-03-31' );
//$rangeEnd   = new DateTime( '2013-04-05' );

// MAY 2013 Range (Working)
//$rangeStart = new DateTime( '2013-04-28' );
//$rangeEnd   = new DateTime( '2013-06-02' );

// Event date start
$eventStart = new DateTime( '2012-10-29' );
$recurTimes = 1;

// Loop thru the days of month
while( $rangeStart->format('U') <= $rangeEnd->format('U') ) {
    $currView = mktime( 0, 0, 0, $rangeStart->format('m'), $eventStart->format('d'), $rangeStart->format('Y') );
    $interval = round(($currView-$eventStart->format('U')) / 60 / 60 / 24 / 30);

    $monthsAway = $eventStart->format('m')+$interval;
    $recurMonth = $eventStart->format('m')%$recurTimes;

    if( $monthsAway%$recurTimes == $recurMonth ) {
        $nextRecur = getNextRecur( $eventStart->format('U'), $interval );
        echo date( 'Y-m-d', $nextRecur ) . '<br />';
    }
    $rangeStart->modify('+1 month');
}


// function to add 1 month with leap year in consideration
function getNextRecur( $baseTime=null, $months=1 ) {
    if( is_null( $baseTime ) ) $baseTime = time( );
    $xMonths = strtotime( '+' . $months . ' months', $baseTime );
    $before = (int)date( 'm', $baseTime )+12*(int)date( 'Y', $baseTime );
    $after  = (int)date( 'm', $xMonths )+12*(int)date( 'Y', $xMonths );
    if( $after > $months+$before ) {
        $xMonths = strtotime( date('Ym01His', $xMonths) . ' -1 day' );
    }
    return $xMonths;
}
?>
Foi útil?

Solução

You can try

Example 1

$rangeStart = new DateTime('2012-12-30');
$rangeEnd = new DateTime('2013-02-03');
$eventStart = new DateTime('2012-10-29');
var_dump(getRange($rangeStart, $rangeEnd, $eventStart));

Output

array
  0 => string '2013-01-29' (length=10)

Example 2

$rangeStart = new DateTime('2012-01-27');
$rangeEnd = new DateTime('2013-03-03');
$eventStart = new DateTime('2012-10-29');
var_dump(getRange($rangeStart, $rangeEnd, $eventStart));

Output

array
  0 => string '2012-10-29' (length=10)
  1 => string '2012-11-29' (length=10)
  2 => string '2012-12-29' (length=10)
  3 => string '2013-01-29' (length=10) 

Function Used

function getRange($rangeStart, $rangeEnd, $eventStart, $fixedDay = 29) {
    $lastStart = clone $rangeStart;
    $rangeStart->setDate($eventStart->format('Y'), $eventStart->format('m'), $eventStart->format('d'));
    $list = array();
    while ( $rangeStart <= $rangeEnd ) {
        if ($rangeStart > $rangeEnd)
            break;
        if ($rangeStart->format('m') == 2 || $rangeStart < $lastStart) {
            $rangeStart->modify('+1 month');
            $rangeStart->setDate($rangeStart->format('Y'), $rangeStart->format('m'), $fixedDay);
            continue;
        }
        $list[] = $rangeStart->format("Y-m-d");
        $rangeStart->modify('+1 month');
        $rangeStart->setDate($rangeStart->format('Y'), $rangeStart->format('m'), $fixedDay);
    }
    return $list;
}

Outras dicas

Perhaps there is a reason for your approch, but in my mind it is a bit round the houses. We all know that all the months are constant in days with Febuary being the odd one. So I would sagest that you look at it from that view.

I have not looked at how "EXACTLY" I would do it, but I would simplify it to just working on Febuary. Perhaps you can do a year check to see if it is a leap year first. Then have your code do what you need it to do with Febuarys date inserted.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top