PHP 5.3 DateTime for recurring events

后端 未结 2 801
孤街浪徒
孤街浪徒 2021-02-04 22:21

I have a calendar application which utilizes the newer PHP DateTime classes. I have a way that I handle recurring events, but it seems hack-ish and I wanted to see if you guys h

2条回答
  •  逝去的感伤
    2021-02-04 22:32

    This can be done nicely using a DatePeriod as an Iterator and then filterd to the start date you want to display:

    starttime = $start->getTimestamp();
        }
        public function accept() {
            return ($this->starttime < $this->current()->getTimestamp());
        }
    }
    
    $db = new DateTime( '2009-11-16' );
    $de = new DateTime( '2020-12-31 23:59:59' );
    $di = DateInterval::createFromDateString( '+3 days' );
    $df = new DateFilterIterator(
        new DatePeriod( $db, $di, $de ),
        new DateTime( '2009-12-01') );
    
    foreach ( $df as $dt )
    {
        echo $dt->format( "l Y-m-d H:i:s\n" );
    }
    ?>
    

提交回复
热议问题