Loop date with day, using foreach

More than 1 year has passed since last update.


Loop Date with Foreach

Usage :


We have a lot of data for user register action,

now I want to know how money people sign up on our system each day.

and I need do this in code


Method 1

$begin = new DateTime( '2010-05-01' );

$end = new DateTime( '2010-05-10' );

$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);

foreach ( $period as $dt )
echo $dt-> format( "Y-m-d" );

Method 2

$startTime = strtotime( '2010-05-01 12:00' );

$endTime = strtotime( '2010-05-10 12:00' );

// Loop between timestamps, 24 hours at a time
for ( $i = $startTime; $i < = $endTime; $i = $i + 86400 ) {
$thisDate = date( 'Y-m-d', $i ); // 2010-05-01, 2010-05-02, etc
}