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

如何使用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.

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类