drqn5418 2014-01-28 20:03
浏览 138
已采纳

如何使用DateTime将两个日期之间的日期转换为数组

What i'm trying to do here is to get dates with specific criteria from 3 months range, and put them into arrays.

to get dates:

$begin = new DateTime( 'NOW' );
$end = new DateTime('NOW');
$end->modify('+6 month');

$interval = new DateInterval('P1D');
$period = new DatePeriod($begin, $interval ,$end);

filters to get my dates of criteria:

foreach ( $period as $dt ){
    if ((!($dt->format("l") === 'Saturday') || ($dt->format("l") === 'Sunday')) && ($dt->format("d") == '14')){
        $meeting = $dt->format( "Y-m-d" );
        echo $dt->format( "Y-m-d" ) . '<br />';

    }

    if((($dt->format("l") === 'Saturday') || ($dt->format("l") === 'Sunday')) && ($dt->format("d") == '14')){
        $dt->modify('Next monday');
        echo $dt->format( "Y-m-d" ) . '<br />';

    }   
}

echo '<hr>';
$interval_2 = new DateInterval('P1M');
$period_2 = new DatePeriod($begin, $interval_2 ,$end);

foreach ( $period_2 as $dt ){
    $dt->modify('last day of this month');  
    //echo $dt->format( "l Y-m-d
" ) . '<br />';
    if(($dt->format("l") === 'Saturday') || ($dt->format("l") === 'Sunday') || ($dt->format("l") === 'Friday')){
        $dt->modify('previous Thursday');
        echo $dt->format( "Y-m-d" ) . '<br />';
    } else {
        echo $dt->format( "Y-m-d" ) . '<br />';
    }


}

well all this works perfect, but they are just displaying, and i want to put them in array to get them into CSV file with fputcsv so i need them like:

Month, first_date, second_date

how can i achieve that?

  • 写回答

1条回答 默认 最新

  • dpv50040 2014-01-28 21:30
    关注

    resolved my issue by declaring:

    $meet = array();
    $test = array();
    

    and after that in each if:

    $meet[] = $dt->format( "Y-m-d" );
    $test[] = $dt->format( "Y-m-d" );
    

    and this way if you make a var_dump, it will have an array for each one of them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?