0

I have a data like this:

$date = '01-01-2014';
$time = '15:20:00';
$location = 'New Delhi';
$recursive = '1';
.........................
.........................  // other data

$recursive = 1 means weekly and 2 means monthly.

Now what i am tring to do is if recursive type is weekly then add 7 days into it till 3 months and if recursive type is monthly then add 1 month into it till 3 months.

Exa:1 $date = '01-01-2014' and $recursive = '1'

Means in above example $recursive is weekly, so get a recursive date for January, February and March.
So the expected results are:

01-01-2014, 08-01-2014, 15-01-2014, 22-01-2014, 29-01-2014 (recursiive date in january)
05-02-2014, 12-02-2014, 19-02-2014, 26-02-2014 (recursiive date in february)
05-03-2014, 12-03-2014, 19-03-2014, 26-03-2014 (recursiive date in march)

Exa 2: $date = 15-04-2014 and $recursive = 1 then get recursive date for April, May and June.

output:

15-04-2014,22-04-2014,29-04-2014 (recursive date in april)
06-05-2014,13-05-2014,20-05-2014,27-05-2014 (recursive date in may)
03-06-2014,10-06-2014,17-06-2014,24-06-2014 (recursive date in june)

Exa 3 : $date = 01-01-2014 and $recursive = 2 then get recursive date for April, May and June.

This is monthly recursive, means add 1 month into it.

output:

01-01-2014 
01-02-2014 (recursiive date in february)
01-03-2014 (recursiive date in march)

then i want to insert these dates with other data into database table.

so how to achive above things? should i write logic in php or use mysql query for it?

Thanks in advance.

SIDENOTE: currently i am using this accepted answer. but now i am trying to change that.

0

2 Answers 2

1

so basically what you want to do is something like this.

//you have to have a default time zone set.. so i just did this you should already have it in your .ini file
date_default_timezone_set('America/Los_Angeles');
$date = '01-01-2014';
$time = '15:20:00';
$location = 'New Delhi';
$recursive = '1';

//set your start date and end date
$startdate = date_create($date);
$enddate = date_create($date);
$enddate = date_add($enddate,date_interval_create_from_date_string("3 months"));

//set the interval string
if($recursive == '1'){
  $str = "7 days";
} else {
  $str = "1 month";
}
function recursivefunc($str, $start, $end){
  //if the start is equal or bigger than end pop out.
  $s = date_format($start,"Y/m/d");
  $e = date_format($end,"Y/m/d");
  if(strtotime($s) >= strtotime($e)){
    return 1;
  }
  echo date_format($start,"Y/m/d"), '<br>'; //print out the starting date for each loop
  $newDate = date_add($start,date_interval_create_from_date_string($str)); //increment the start
  recursiveFunc($str, $newDate, $end); //call the function again

}
recursiveFunc($str, $startdate, $enddate); // initial call to the function
Sign up to request clarification or add additional context in comments.

Comments

0

This is how i solve it.

$date = '01-01-2014';
$location = 'New Delhi';

$start = strtotime("+2 months", strtotime($date));  //start date
$end_date = date("Y-m-t", $start);   // last day of end month
$end = strtotime($end_date);   //last date

/* if recursion type is weekly */
if($json['recurrence'] == "1")
{
    for($dd=$start; $dd<=$end;)
    {
        $new_date = date('Y-m-d', $dd);
        $data[] = array(
            'date' => $new_date,
            'location' => $location
        );
        $dd = strtotime("+7 day", $dd);  // add 7 days
    }
}

if($json['recurrence'] == "2")
{
    for($dd=$start; $dd<=$end;)
    {
        $new_date = date('Y-m-d', $dd);
        $data[] = array(
            'date' => $new_date,
            'location' => $location
        );
        $dd = strtotime("+1 month", $dd);  //add 1 month
    }
}


echo "<pre>";
print_r($data);

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.