Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get dates between two dates into array with 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\n" ) . '<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?

like image 374
Liza Avatar asked Mar 21 '26 03:03

Liza


1 Answers

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.

like image 189
Liza Avatar answered Mar 22 '26 16:03

Liza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!