2012-06-13 15 views
6

Sto provando molti approcci ma poi rimango bloccato a metà strada.PHP. Come ottenere un evento ricorrente ogni due mesi?

Diciamo che l'ordine è stato creato oggi. Devo visualizzare quando avverrà il prossimo ordine ricorrente. Quindi ho un ordine creato il 13 giugno 2012. Quindi ho impostato il programma su un ordine ricorrente ogni due mesi. Come calcolare quando si verificherà il prossimo ordine ricorrente? La risposta è il 1 ° agosto.

Se qualcuno può delineare un approccio, sarebbe molto utile, non deve essere codice. Questo è ciò che ho finora ...

// 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>'; 

Non voglio trovare semplicemente ciò che è la data in due mesi da oggi. Diciamo che l'ordine è stato creato il 1 giugno. Il prossimo ordine ricorrente è il 1 ° agosto. Diciamo ora, oggi è il 1 ° settembre, ma il prossimo ordine ricorrente è il 1 ° ottobre. Vedi il mio dilemma?

+0

Vuoi ** solo ** la prossima data ricorrente da oggi, o tutte le date ricorrenti dalla creazione fino ae compresa la successiva da oggi? – salathe

+0

Non vedo il tuo dilemma. Perché è importante il 1 ° settembre? Come si relaziona con il problema degli ordini ricorrenti? – afuzzyllama

+0

Perché se l'ordine è stato creato oggi 13 giugno è facile trovare il prossimo ordine ricorrente. È anche facile trovare una serie di tutti i prossimi 6 ordini ricorrenti. Ora, se oggi fosse il 1 ° settembre, come calcoli il prossimo ordine ricorrente? Tutto ciò che deve essere calcolato è UNA data, solo il prossimo ordine ricorrente. – leonel

risposta

5

Basta prendere il mese corrente, quindi dal momento che è giugno, otteniamo 6. 6 mod 2 == 0. Il mese prossimo è luglio, otteniamo 7. 7 mod 2 == 1.

Quindi basta controllare se current month % 2 == (first month % 2).

Quindi basta controllare se è il 1 ° del mese.

Nel modulo PHP è definito con il simbolo della percentuale.

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

è bimestrale, ogni due mesi – leonel

+0

Esattamente, questo metodo funzionerebbe. Se avessi iniziato a giugno (6), sarebbe saltato luglio e poi agosto (8) avrebbe ripresentato l'evento. (6% 2) == (8% 2) –

+0

E se dovessi avviarlo a luglio (7), si ripresenterebbe a settembre perché '9% 2' e' 7% 2' sono uguali, ma sarebbe salta agosto poiché '8% 2' non è uguale a' 7% 2' –

2

ho ottenuto questo:

$today = new DateTime(); 
$target_date = $today->modify("first day of +2 months"); 

echo "Your event is on " . $target_date->format("d/m/Y") . "!"; 
2

strtotime in soccorso:

<?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

Diciamo che si desidera che i prossimi sei ordini:

$order_date = '6/13/2012'; 
$start  = date('Y-m-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); 
} 

questo può, ovviamente, , essere migliorato, ma dovrebbe iniziare. ..

4

È possibile trovare la libreria When utile per questo (io sono l'autore).

Qui è il codice che vi porterà il prossimo 2 ricorrenti date mensili (a partire dalla data di oggi):

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 

Prendendo questo un ulteriore passo avanti, è probabile che vogliono solo trovare i 2 primi giorni:

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 

Inoltre, il codice è attualmente in fase di riscrittura: funziona bene ma è un po 'confuso e non ben documentato.

Problemi correlati