1

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?

1

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3
$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;
}

1 Comment

This is how I would do it as well. Don't forget the closing curly brace that fell outside the code box! :)
1
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.

2 Comments

Just your function names are not the same :D
You are not getting the desired result with this function.
1
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' );

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.