Get dates between 2 dates with PHP

Free Scripts :: PHP :: Date and Time :: Get dates between 2 dates with PHP

Author: Salman Javaid

Website: http://www.salman.be

  • How to get dates between 2 dates with PHP.
  • Get date range between two dates.
  • Show days between 2 dates.

This function accepts start date and end date parameter and and returns all dates between them.

Function

function getDatesBetween2Dates($startTime, $endTime) {
    $day = 86400;
    $format = 'Y-m-d';
    $startTime = strtotime($startTime);
    $endTime = strtotime($endTime);
    $numDays = round(($endTime - $startTime) / $day) + 1;
    $days = array();
        
    for ($i = 0; $i < $numDays; $i++) {
        $days[] = date($format, ($startTime + ($i * $day)));
    }
        
    return $days;
}

Calling The Function

$days = getDatesBetween2Dates('2010/04/21', '2010/04/25');

foreach($days as $key => $value){

    echo $value . ', ';

}

Output

2010-04-21, 2010-04-22, 2010-04-23, 2010-04-24, 2010-04-25,

Comments

No comments. Be the first one.

Leave a comment

Full Name*
Comments*
Security Code*

Our Partners

Getty Icons  Free MP3 Songs
Free High Quality Images  Learn Free Languages 
Opal Hosting  R-Tech Solutions
Virtualxone
  • Submit a Script

    Click here to submit your script.