2013-06-27 4 views
0

Я немного array-ed с этим, и я надеюсь, что кто-то может мне помочь. У меня есть несколько переменных, и я немного застрял в использовании массивов в выражении if.расчет еженедельной серии с php, если заявление

То, что я хотел бы узнать, если мой relocation_date меньше, то notified_time (из сгенерированной серии), то дайте мне old_meter_id еще meter_id

$meter_id   = 1393; 
$notified_time = '2013-05-01 09:53'; 
$completed_time = '2013-05-01 11:52'; 
$relocation_date = '2013-04-24 00:00'; 
$old_meter_id = 1832; 

$notified_time = strtotime($notified_time); 
$completed_time = strtotime($completed_time); 
$relocation_date = date($relocation_date); 

$combined = array(); 

for($i=0;$i<=10;$i++) 
{  
    $start_series = strtotime("- $i weeks", $notified_time); 
    $end_series = strtotime("- $i weeks", $completed_time); 

    $combined[] = array('start_time' => date('d-m-Y H:i:s',$start_series), 
         'end_time'  => date('d-m-Y H:i:s',$end_series), 
         'relocation' => $relocation_date, 
         'meter_id'  => $meter_id, 
         'old_meter_id' => $old_meter_id, 
         ); 
} 

образца ожидается выход быть как:

Array 
(
    [0] => Array 
     (
      [start_time] => 01-05-2013 09:53:00 
      [end_time] => 01-05-2013 11:52:00 
      [relocation] => 2013-04-24 00:00 
      [meter_id] => 1393 
     ) 

    [1] => Array 
     (
      [start_time] => 24-04-2013 09:53:00 
      [end_time] => 24-04-2013 11:52:00 
      [relocation] => 2013-04-24 00:00 
      [meter_id] => 1832 
     ) 

..... 

Спасибо заранее!

ответ

1

Вы можете проверить его в петле, как

for($i=0;$i<=10;$i++) 
{  
    $start_series = strtotime("- $i weeks", $notified_time); 
    $end_series = strtotime("- $i weeks", $completed_time); 
if(strtotime($relocation_date) < $start_series){ 
    $combined[] = array('start_time' => date('d-m-Y H:i:s',$start_series), 
         'end_time'  => date('d-m-Y H:i:s',$end_series), 
         'relocation' => $relocation_date, 
         'meter_id'  =>$old_meter_id,         
         ); 
}else{ 
    $combined[] = array('start_time' => date('d-m-Y H:i:s',$start_series), 
         'end_time'  => date('d-m-Y H:i:s',$end_series), 
         'relocation' => $relocation_date, 
         'meter_id'  =>$meter_id,         
         ); 
} 

} 

Надежда это решение, которое вы ищете

+0

Это точно, спасибо :) @dianuj – Glicious

+0

Приглашаем Вас :) –