0

I have the following code. It works to add days (daysBetween) to a date (startDate). However, I want to REPEAT it until it reaches the end date. How can I do this??

$startDate = "2009-10-11";
$endDate = "2010-01-20";
$daysBetween = 10;

function addDayswithdate($date,$days){

  $date = strtotime("+".$days." days", strtotime($date));
  return  date("Y-m-d", $date);

}

$date = addDayswithdate($startDate,$daysBetween);
5
  • 2
    What is the expected result? Commented Apr 24, 2014 at 10:00
  • an array of all the dates Commented Apr 24, 2014 at 10:02
  • @alex you can do that with recursive function. See my answer and demo for further detail Commented Apr 24, 2014 at 10:28
  • Look into PHP's DateTime, DateInterval, and DatePeriod documentation for the tools you need to do this job. Commented Apr 25, 2014 at 6:43
  • Please clarify your description. It's confusing! Commented Apr 25, 2014 at 6:47

2 Answers 2

3

You can use following function;

<?php

$startDate = "2009-10-11";
$endDate = "2010-01-20";
$daysBetween = 10;
$finalResult = array();
function addDayswithdate($date,$days, $endDate, &$finalResult){
  $tempDate = strtotime($date);
  $tempDate += 3600*24*$days;
  if ($tempDate < strtotime($endDate)) {
    $finalResult[] = date("Y-m-d", $tempDate);
    addDayswithdate(date("Y-m-d", $tempDate), $days, $endDate, $finalResult);
  } else {
    return true;        
  }
}

addDayswithdate($startDate,$daysBetween, $endDate, $finalResult);
var_dump($finalResult);

Here is a working demo: Demo

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

4 Comments

Thats not what it is supposed to do. It is supposed to add a date from every 10 days starting from startDate till endDate and add to an array
Could you please give example output in your question?
if start date is 1-10-2014, end date is 30-10-2014 and daysbetween is 10,I want these days: 10-10-2014 and 20-10-2014
@alex: Your question was lacking information/input. Yet two people invested quite a bit of time in solving your problem. At least accept either one of the answers and give upvote where upvote is due.
1
   function addDayswithdate($from, $to, $interval){

        $result = array();
        while(true)
        {
            $dateTemp = strtotime("+".$interval." days", strtotime($from));
            if(strtotime($dateTemp) > strtotime($to)) 
                break;
            $result[] = date("Y-m-d", $dateTemp);
            $interval += $interval;
        }
        return  $result;

    }

7 Comments

It returns this. array(10) { [0]=> string(10) "2014-04-01" [1]=> string(10) "1970-01-02" [2]=> string(10) "1970-01-03" [3]=> string(10) "2014-04-27" [4]=> string(10) "1970-01-05" [5]=> string(10) "1970-01-06" [6]=> string(10) "1970-01-07" [7]=> string(10) "1970-01-08" [8]=> string(10) "1970-01-09" [9]=> string(10) "1970-01-10" }
but now it returns this array(10) { [0]=> string(10) "2014-04-01" [1]=> string(10) "2014-04-02" [2]=> string(10) "2014-04-03" [3]=> string(10) "2014-04-04" [4]=> string(10) "2014-04-05" [5]=> string(10) "2014-04-06" [6]=> string(10) "2014-04-07" [7]=> string(10) "2014-04-08" [8]=> string(10) "2014-04-09" [9]=> string(10) "2014-04-10" }
What I am trying to do is add a date to the array AFTER the number of days ($days)
you needed all the dates plus the given number,right?
no... if start date is 1-10-2014, end date is 30-10-2014 and days is 10,I want these days: 10-10-2014 and 20-10-2014
|

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.