PHP How to get a two-month recurring event?

I try many approaches, but then I got stuck halfway.

Say the order was created today. I need to display when the next recurring order occurs. So I have an order created on June 13, 2012. Then I set the schedule to a two-month periodic order, every 1st month. How to calculate when the next recurring order will occur? The answer is August 1.

If someone can outline the approach, it will be very useful, it should not be code. This is what I still have ...

// first, get starting date $start_date_month = date('m', strtotime($start_date)); // get this year $this_year = date('Y'); // if this month is december, next month is january $this_month = date('m', $timestamp_month); if($this_month == 12){ $next_month = 1; // if this month is not december add 1 to get next month }else{ $next_month = $this_month + 1; } // get array of months where recurring orders will happen $months = array(); for ($i=1; $i<=6; $i++) { $add_month = $start_date_month+(2*$i); // 2, 4, 6, 8, 10, 12 if($add_month == 13){$add_month = 1;$year = $this_year+1;} elseif($add_month == 14){$add_month = 2;$year = $this_year+1;} elseif($add_month == 15){$add_month = 3;$year = $this_year+1;} elseif($add_month == 16){$add_month = 4;$year = $this_year+1;} elseif($add_month == 17){$add_month = 5;$year = $this_year+1;} elseif($add_month == 18){$add_month = 6;$year = $this_year+1;} elseif($add_month == 19){$add_month = 7;$year = $this_year+1;} elseif($add_month == 20){$add_month = 8;$year = $this_year+1;} else{$year = $this_year;} echo $what_day.'-'.$add_month.'-'.$year.'<br />'; $months[] = $add_month; } echo '<pre>'; print_r($months); echo '</pre>'; 

I don’t want to just find what it is in two months. Let's say the order created on June 1. The next recurring order is August 1. Then let's say today, September 1, but the next recurring order is October 1. See My Dilemma?

+6
source share
5 answers

Just take the current month, so from June we get 6. 6 mod 2 == 0 . The next month is July, we get 7. 7 mod 2 == 1 .

So just check, <<22>.

Then just check if it's the first month.

The PHP module defines a percentage symbol.

 $month = date('n'); $createdMonth = 6; if($month % 2 == $createdMonth % 2){ // stuff } 
+5
source

You can find a useful library called When (I am the author).

Here is the code that will give you the following 2 recurring monthly dates (from today's date):

 include 'When.php'; $r = new When(); $r->recur(new DateTime(), 'monthly') ->count(2) ->interval(2) // every other month ->bymonthday(array(1)); while($result = $r->next()) { echo $result->format('c') . '<br />'; } // output // 2012-08-01T13:33:33-04:00 // 2012-10-01T13:33:33-04:00 

Following this step, you will most likely want to find the first 2 working days:

 include 'When.php'; $r = new When(); $r->recur(new DateTime(), 'monthly') ->count(2) ->interval(2) // every other month ->byday(array('MO', 'TU', 'WE', 'TH', 'FR')) // week days only ->bymonthday(array(1, 2, 3)) // the first weekday will fall on one of these days ->bysetpos(array(1)); // only return one per month while($result = $r->next()) { echo $result->format('c') . '<br />'; } // output // 2012-08-01T13:33:33-04:00 // 2012-10-01T13:33:33-04:00 

Also note that the code is currently under rewriting - it works well, but it is a bit confusing and not documented.

+4
source

I got it:

 $today = new DateTime(); $target_date = $today->modify("first day of +2 months"); echo "Your event is on " . $target_date->format("d/m/Y") . "!"; 
+2
source

strtotime to the rescue:

 <?php date_default_timezone_set('Europe/London'); $d = new DateTime('2012-01-31'); $d->modify('first day of +2 months'); echo $d->format('r'), "\n"; ?> 
+2
source

Let's say you want the following six orders of magnitude:

 $order_date = '6/13/2012'; $start = date('Ym-01', strtotime($order_date)); $order_count = 6; $future_orders = array(); $next = strtotime('+2 months', strtotime($start)); while(count($future_orders) < $order_count){ $future_orders[] = date('m/d/Y',$next); $next = strtotime('+2 months', $next); } 

This can obviously be improved, but you should start ...

+2
source

Source: https://habr.com/ru/post/918024/


All Articles