Date Periods Between Two Dates


In this tutorial we will get all days between two dates in PHP 5 and above.

$begin = new DateTime( '2016-02-01' );// Start date
$end = new DateTime( '2016-03-01' );// End date

// We need to get each days so add a date intveral of 1 day
$interval = DateInterval::createFromDateString('1 day');
// Create a new date period.
$period = new DatePeriod($begin, $interval, $end);

// Loop through each dates and format
foreach ( $period as $dt )  
echo $dt->format( "l Y-m-d H:i:s\n" );

That’s it. A simple but useful set of codes. Leave a comment if you find this useful.

Please Post Your Comments & Reviews

Welcome to Western Studios

We provide professional website and mobile softwares including Joomla, Wordpress and more.