user1137370
user1137370

Reputation: 137

array with dates between two different dates

I am currently working on a calender script for my personal use. Therefore I need your help :-)

I have two dates in format YYYY-MM-DD.

for example:

2012-05-12 and
2012-05-16

What I need is the dates between them:

2012-05-13
2012-05-14
2012-05-15

The output should be in an array. I dont now how to start anyway... so do u have a hint?

Upvotes: 1

Views: 3718

Answers (5)

Razvan
Razvan

Reputation: 10093

$date =  date("Y-m-d", strtotime("2012-05-12"));
$final_date = date("Y-m-d", strtotime("2012-05-16"));
while($date < $final_date){
   $date = date("Y-m-d", strtotime($date . " +1 day"));
   $dates[] = $date;
}

Upvotes: 3

Ovidiu
Ovidiu

Reputation: 2931

function getDates($startTime, $endTime) {
    $day = 86400;
    $format = 'Y-m-d';
    $startTime = strtotime($startTime);
    $endTime = strtotime($endTime);
    //$numDays = round(($endTime - $startTime) / $day) + 1;
    $numDays = round(($endTime - $startTime) / $day); // remove increment 

    $days = array();

    for ($i = 1; $i < $numDays; $i++) { //change $i to 1
        $days[] = date($format, ($startTime + ($i * $day)));
    }

    return $days;
}

$days = getDates('2012-05-12', '2012-05-16');

Output:

Array
(
    [0] => 2012-05-13
    [1] => 2012-05-14
    [2] => 2012-05-15
)

As Dan Lee said, I changed the function to get only the date interval, excluding the first and last day from array.

Upvotes: 1

user1437178
user1437178

Reputation:

Here is the code:

function createDateRangeArray($strDateFrom,$strDateTo)
    {
        // takes two dates formatted as YYYY-MM-DD and creates an
        // inclusive array of the dates between the from and to dates.

        // could test validity of dates here but I'm already doing
        // that in the main script

        $aryRange=array();

        $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
        $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

        if ($iDateTo>=$iDateFrom)
        {
            array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
            while ($iDateFrom<$iDateTo)
            {
                $iDateFrom+=86400; // add 24 hours
                array_push($aryRange,date('Y-m-d',$iDateFrom));
            }
        }
        return $aryRange;
    }

I have taken a reference from this website.

Upvotes: 0

dan-lee
dan-lee

Reputation: 14492

Here's the OOP approach, you should use it:

$a = new DateTime('2012-05-12');
$b = new DateTime('2012-05-16');

// to exclude the end date (so you just get dates between start and end date):
// $b->modify('-1 day');

$period = new DatePeriod($a, new DateInterval('P1D'), $b, DatePeriod::EXCLUDE_START_DATE);

foreach($period as $dt) {
  echo $dt->format('Y-m-d');
}

For further reading see DateTime, DateInterval and DatePeriod.

Upvotes: 5

Peon
Peon

Reputation: 8020

function GetDays ($sStartDate, $sEndDate ) {  
  $sStartDate = gmdate( 'Y-m-d', strtotime( $sStartDate ) );  
  $sEndDate = gmdate( 'Y-m-d', strtotime( $sEndDate ) );  

  $aDays[] = $sStartDate;  

  $sCurrentDate = $sStartDate;  

  while( $sCurrentDate < $sEndDate ) {  
    $sCurrentDate = gmdate( 'Y-m-d', strtotime( '+1 day', strtotime( $sCurrentDate) ) );  

    $aDays[] = $sCurrentDate;  
  }  

  return $aDays;  
}  

print_r ( '2012-05-12', '2012-05-16' );

Upvotes: 1

Related Questions