0

我对此有点困惑,我希望有人可以帮助我。我有一些变量,并且有点坚持在 if 语句中使用数组。

我想知道的是,如果我的 relocation_date 小于 notify_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
        )

.....

提前致谢!

4

1 回答 1

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,                                
                         );
}

}

希望这是您正在寻找的解决方案

于 2013-06-27T07:40:56.147 回答