【问题标题】:Generate an array using DateInterval and DatePeriod for shifts使用 DateInterval 和 DatePeriod 为班次生成一个数组
【发布时间】:2014-07-02 16:09:24
【问题描述】:

我正在使用 DateInterval 和 DatePeriod 在医疗应用程序中生成班次

$begin = new DateTime('2012-08-01 08:00');
$end = new DateTime('2012-08-01 12:00');
$interval = new DateInterval('PT45M');
$daterange = new DatePeriod($begin, $interval ,$end);

这些函数允许我生成以下数组

Array
(
    [0] => 08:00
    [1] => 08:45
    [2] => 09:30
    [3] => 10:15
    [4] => 11:00
    [5] => 11:45
)

哪些函数以及我怎样才能使最终数组像这样完成并分块?

Array
(
    [0] => Array
        (
            [0] => 08:00
            [1] => 08:45
        )

    [1] => Array
        (
            [0] => 08:45
            [1] => 09:30
        )

    [2] => Array
        (
            [0] => 09:30
            [1] => 10:15
        )

    [3] => Array
        (
            [0] => 10:15
            [1] => 11:00
        )

    [4] => Array
        (
            [0] => 11:00
            [1] => 11:45
        )
)

谢谢

【问题讨论】:

    标签: php arrays date intervals chunks


    【解决方案1】:
    $previous = null;
    $dates    = array();
    foreach ($period as $dt) {
        $current = $dt->format("H:i");
        if (!empty($previous)) {
            $show = new DateTime($current);
            $dates[] = array($previous, $show->format('H:i'));
        }
        $previous = $current;
    }
    

    【讨论】:

    • 嗨,约翰。我已经尝试过并且效果很好,但是如果我将间隔设置为 PT30M 并开始:2012-08-01 08:00 结束:2012-08-01 10:00,它不会考虑“最终班次” ,但我认为这可能是由于 DatePeriod 函数中的错误?
    • @Enrique DatePeriod 将跳过最后一次迭代,如果它正好是间隔的长度。将一秒或一分钟添加到您的 $end 即可。
    • 非常感谢约翰。工作完美,但希望有一天 DatePeriod 有一个允许包含最后一个间隔的参数
    【解决方案2】:

    使用DateTime 函数检索您的数组后。只需使用代码即插即用。 :)

    $retrieveArray = array('08:00','08:45','09:30','10:15','11:00','11:45'); // Sample Array, Assumed mentioned in Question.
    
    $final = array();
    for ($i=0; $i < count($retrieveArray)-1; $i++) { 
        $j = $i+1;
        $final[$i] = array($retrieveArray[$i], $retrieveArray[$j]);
    }
    

    使用var_dump($final);查看数组结果。

    期望的输出

    Array
    (
        [0] => Array
            (
                [0] => 08:00
                [1] => 08:45
            )
    
        [1] => Array
            (
                [0] => 08:45
                [1] => 09:30
            )
    
        [2] => Array
            (
                [0] => 09:30
                [1] => 10:15
            )
    
        [3] => Array
            (
                [0] => 10:15
                [1] => 11:00
            )
    
        [4] => Array
            (
                [0] => 11:00
                [1] => 11:45
            )
    
    )
    

    编辑:

    添加最后一个班次

    @jhon 是对的。如果它与间隔的长度完全匹配,它将跳过最后一个边界值。 根据您的需要,我建议添加 +1 min 到您的结束时间。这是一个调整/修复,但服务于目的。

    $begin = new DateTime('2012-08-01 08:00');
    $end = new DateTime('2012-08-01 10:00');
    $end = $end->modify( '+1 minute' ); // Tweak, to add last shift value.
    $interval = new DateInterval('PT30M');
    $daterange = new DatePeriod($begin, $interval ,$end);
    

    检查输出

    foreach ( $daterange as $dt ){
        echo '<pre>';
        echo $dt->format( "H:i" );
        echo '</pre>';
    }
    

    如果与区间长度完全匹配,则包含最后一个边界值。

    【讨论】:

    • 嗨阿布希内特。我已经尝试过您的代码,它运行良好,但如果我将间隔设置为 PT30M 并设置 $begin: 2012-08-01 08:00, $end: 2012-08-01 10:00,它不会考虑计算“最终班次”,即 09:30 至 10:00。你认为这可能是一个 DatePeriod 错误,你怎么能解决?我必须能够更改间隔时间、开始和结束时间,并且能够完美地生成班次。谢谢
    • @Enrique 刚刚更新了我的答案。我建议您在最后一次添加 +1 分钟。在不超过区间值之前添加值是可以的。
    • 非常感谢 Abhineet。完美运行!
    • 乐于帮助@Enrique。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2015-03-04
    • 2018-03-14
    相关资源
    最近更新 更多