2013-04-18 4 views
6

Ho un problema strano con le date degli eventi e ho provato a risolverlo, ma non sono riuscito a farlo.Omettere i mesi e gli anni ridondanti nell'elenco di intervalli di date

Vi allego uno screenshot di come voglio per visualizzare i date a pagina: enter image description here

Nella foto il primo evento Deine Energie in azione! è una combinazione di 5 eventi con ogni evento che ha la sua data di inizio e la sua data di fine.

La prima parte dell'evento è un evento di un giorno che inizia il 4 aprile e termina il 4 aprile. Allo stesso modo la seconda parte è il 7 aprile, 3a parte il 9 aprile e la quarta parte il 20 aprile

L'ultima parte inizia il 5 maggio e termina il 10 maggio.

Le date sono memorizzate nel database in questo formato: Sto mostrando le date per l'ultima parte dell'evento. Inizio Evento Data: 2013/05/05 00:00:00 Data Evento fine: 2013-05-10 00:00:00

quindi voglio per visualizzare le date nel formato mostrato in figura.

Ci sono più casi: Prima è se tutte le date sono in arrivo in un solo mese, quindi il nome del mese viene visualizzato alla fine solo una volta. Il secondo è se i mesi vengono cambiati, quindi il nome del mese verrà visualizzato dopo la data in cui il mese è cambiato.

Ricevo le date degli eventi in un ciclo while, quindi come faccio a confrontare la data dell'evento corrente con la data dell'evento in arrivo in un ciclo.

Questo è il codice che ho usato finora per ottenere le date dal database ..

$nid = $row->nid; 
$get_product_id = "SELECT product_id from {uc_product_kits} where nid='$nid'"; 
$res = db_query($get_product_id); 
while ($get_product_id_array_value = db_fetch_array($res)) { 
    $prductid = $get_product_id_array_value['product_id']; 
    $start_date = db_query("select event_start,event_end from {event} where nid=%d",$prductid); 
    $start_date_value = db_fetch_object($start_date); 
    $end_value = $start_date_value->event_start; 

    $event_end_date = $start_date_value->event_end; 
    $TotalStart = date("d M Y", strtotime($end_value)); 
    $TotalEnd = date("d M Y", strtotime($event_end_date)); 
    $onlyMonthStart = date("M", strtotime($end_value)); 
    $onlyMonthEnd = date("M", strtotime($event_end_date)); 
    //$groupMonth = db_query("select event_start,event_end, month from {event} where nid=%d group by ",$prductid); 
    if($TotalStart == $TotalEnd){ 
     $startDay = date("d", strtotime($end_value)); 
     $startMonth = date("M", strtotime($end_value)); 
     if(in_array($startMonth,$newMonth)) { 
      echo $onlstartdate; 
     } 
     else { 
       $onlstartdate = date("d", strtotime($end_value)); 
      echo $onlstartdate; 
      $tempStorage[] = $startMonth 
     } 
     //$newMonth[] = $startMonth; 
    } 
} 

risposta

4

più semplice sarebbe quella di raccogliere prima tutti i dati dalla query in es array.

Solo dopo l'iterazione sull'array. Avere tutti i dati insieme ti permetterà di confrontare due intervalli di date consecutive per decidere il livello di dettagli che devi stampare per ciascuno.

esempio commentato:

// collect data from SQL query into structure like this: 

$events = array(
    array("event_start" => "2013-4-4", "event_end" => "2013-4-4"), 
    array("event_start" => "2013-4-7", "event_end" => "2013-4-7"), 
    array("event_start" => "2013-4-9", "event_end" => "2013-4-9"), 
    array("event_start" => "2013-4-20", "event_end" => "2013-4-20"), 
    array("event_start" => "2013-5-5", "event_end" => "2013-5-10"), 
    array("event_start" => "2014-1-1", "event_end" => "2014-1-2"), 
); 

// the actual code for range list generation: 

for ($i = 0; $i < count($events); $i++) 
{ 
    // parse start and end of this range 
    $this_event = $events[$i]; 
    $this_start_date = strtotime($this_event["event_start"]); 
    $this_end_date = strtotime($this_event["event_end"]); 

    // extract months and years 
    $this_start_month = date("M", $this_start_date); 
    $this_end_month = date("M", $this_end_date); 
    $this_start_year = date("Y", $this_start_date); 
    $this_end_year = date("Y", $this_end_date); 

    $last = ($i == count($events) - 1); 
    // parse start and end of next range, if any 
    if (!$last) 
    { 
     $next_event = $events[$i + 1]; 
     $next_start_date = strtotime($next_event["event_start"]); 
     $next_end_date = strtotime($next_event["event_end"]); 

     $next_start_month = date("M", $next_start_date); 
     $next_end_month = date("M", $next_end_date); 
     $next_start_year = date("Y", $next_start_date); 
     $next_end_year = date("Y", $next_end_date); 
    } 

    // ranges with different starting and ending months always go 
    // on their own line 
    if (($this_start_month != $this_end_month) || 
     ($this_start_year != $this_end_year)) 
    { 
     echo date("j M", $this_start_date); 
     // print starting year only if it differs from ending year 
     if ($this_start_year != $this_end_year) 
     { 
      echo " ".date("Y", $this_start_date); 
     } 
     echo "-".date("j M Y", $this_end_year)." <br/>\n"; 
    } 
    else 
    { 
     // this is range starting and ending in the same month 

     echo date("j", $this_start_date); 
     // different starting and ending day 
     if ($this_start_date != $this_end_date) 
     { 
      echo "-".date("j", $this_end_date); 
     } 

     $newline = false; 
     // print month for the last range; 
     // and for any range that starts(=ends) in different month 
     // than the next range ends 
     if ($last || 
      ($this_start_month != $next_end_month)) 
     { 
      echo " ".date("M", $this_start_date); 
      $newline = true; 
     } 

     // print year for the last range; 
     // and for any range that starts(=ends) in different year 
     // than next range ends 
     if ($last || 
      ($this_start_year != $next_end_year) || 
      ($next_start_month != $next_end_month)) 
     { 
      echo " ".date("Y", $this_start_date); 
      $newline = true; 
     } 

     if ($newline) 
     { 
      echo " <br/>\n"; 
     } 
     else 
     { 
      // month (and year) will be printed for some future range 
      // on the same line 
      echo ", "; 
     } 
    } 
} 

Questo uscite:

4, 7, 9, 20 Apr <br/> 
5-10 May 2013 <br/> 
1-2 Jan 2014 <br/> 
+0

ho aggiornato la mia risposta con qualche esempio di codice. –

+1

Grazie Martin. Questo ha funzionato per me. –

+0

Prego. Felice, ti ha aiutato. –

0

Ebbene, dal momento che è necessario confrontare il valore da un ciclo ad un altro, che non sarà in grado di utilizzare direttamente echo.

È necessario utilizzare le variabili temporanee. Quindi con il primo ciclo per la data di inizio, si memorizza $tmp_day_1 e $tmp_month_1 quindi con il ciclo di fine data è possibile confrontare entrambi i mesi e verificare se sono diferenti. Quindi è possibile utilizzare echo. Spero di fare il mio punto :)

1

Una possibilità per verificare se è necessario stampare il mese per la voce data corrente è in realtà per il check-nel prossimo voce . Vorrei cercare di spiegare con pseudocodice:

<?php 

    $month = 0; // Initialize $month variable to unset 

    // Loop over all your events 
    foreach($dates as $date) { 

     // Convert $date to a timestamp 

     // If the 'month' of the current $timestamp is unequal to $month 
     // it means we switch months and we have to print the $month first 
     if(date('m', $timestamp) != $month) { 

      echo $month; // Of course format how you want it to be displayed 

      // Set $month to the new month 
      $month = date('m', $timestamp); 
     } 

     // Print the rest of the event, like day numbers here 

    } 
?>