两个日期之间的所有可能日期,间隔为60分钟或可变间隔

时间:2018-07-30 03:53:09

标签: php

我的约会系统需要一些改进思路。

我有这个mysql查询结果,这意味着空闲间隔时间:

hours        freeend
09:00:00     12:00:01
14:00:00     15:00:01
16:00:00     19:00:01

和时间间隔,比方说120分钟。 (存储在变量$ d中)

这是我的显示间隔时间的php代码,但不是100%正确...

$d = 120; // let's say
$nr = $result->num_rows;

while($row = mysqli_fetch_array($result)) {

$start    = new DateTime($row['hours']);
$end      = new DateTime($row['freeend']); // add 1 second because last one is not included in the loop

if ($nr < 2) {$interval = new DateInterval('PT60M');} else {$interval = new DateInterval('PT'.$d.'M');}


$period   = new DatePeriod($start, $interval, $end);

$previous = '';
foreach ($period as $dt) {
    $current = $dt->format("H:i");
    if (!empty($previous)) {

        echo "<label class='btn btn-secondary bhours btn-lg'><input type='radio' name='hours' value='{$previous}' id='{$previous}'>{$previous}</label>";
    }
    $previous = $current;
}

}

它仅返回2个结果:

09:00
16:00

正确的是:

09:00
10:00
16:00
17:00

关于如何实施的任何想法? 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

#include <thrust/gather.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/functional.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <iostream>

struct isWithinThreshold {

    double2 thresholdPoint;

    __host__ __device__ isWithinThreshold(double2 thresholdPoint_) { thresholdPoint = thresholdPoint_; };

    __host__ __device__ bool operator()(const double2 r) {
        return ((r.x > thresholdPoint.x) && (r.y > thresholdPoint.y));
    }
};

/********/
/* MAIN */
/********/
int main()
{
    const int N = 5;
    double2 thresholdPoint = make_double2(0.5, 0.5);

    thrust::host_vector<double2> particleCoordinates(N);
    particleCoordinates[0].x = 0.45;    particleCoordinates[0].y = 0.4;
    particleCoordinates[1].x = 0.1;     particleCoordinates[1].y = 0.3;
    particleCoordinates[2].x = 0.8;     particleCoordinates[2].y = 0.9;
    particleCoordinates[3].x = 0.7;     particleCoordinates[3].y = 0.9;
    particleCoordinates[4].x = 0.7;     particleCoordinates[4].y = 0.45;

    // --- Find out the indices
    thrust::host_vector<int> indices(N);
    thrust::host_vector<int>::iterator end = thrust::copy_if(thrust::make_counting_iterator(0),
        thrust::make_counting_iterator(N),
        particleCoordinates.begin(),
        indices.begin(),
        isWithinThreshold(thresholdPoint));
    int size = end - indices.begin();
    indices.resize(size);

    // --- Fetch values corresponding to the selected indices
    thrust::host_vector<double2> values(size);
    thrust::copy(thrust::make_permutation_iterator(particleCoordinates.begin(), indices.begin()),
        thrust::make_permutation_iterator(particleCoordinates.end(), indices.end()),
        values.begin());

    for (int k = 0; k < size; k++)
        printf("k = %d; index = %d; value.x = %f; value.y = %f\n", k, indices[k], values[k].x, values[k].y);

    return 0;
}

输出

<?php 


$arr = [
            [
                'hours' => '09:00:00',
                'freeend' => '12:00:01'
            ],
            [
                'hours' => '14:00:00',
                'freeend' => '15:00:01'
            ],
            [
                'hours' => '16:00:00',
                'freeend' => '19:00:01'
            ],
            [
                'hours' => '10:00:00',
                'freeend' => '23:00:01'
            ]
        ];

$appointment_duration = new DateInterval('PT2H');
$next_hour = new DateInterval('PT1H');

foreach($arr as $row){
    $start_time = new DateTime($row['hours']);
    $end_time   = new DateTime($row['freeend']);

    echo "Appointments available between $row[hours] and $row[freeend] <br/>";

    $curr_start_time = $start_time;
    $curr_end_time   = new DateTime($start_time->format("H:i:s"));
    $curr_end_time   = $curr_end_time->add($appointment_duration);

    do{
        if($curr_end_time > $end_time){
            echo "$row[hours]-$row[freeend] <br/>";
            break;
        }

        echo $curr_start_time->format("H:i:s"),"-",$curr_end_time->format("H:i:s"),"<br/>";
        $curr_start_time = $curr_start_time->add($next_hour);
        $curr_end_time   = new DateTime($curr_start_time->format("H:i:s"));
        $curr_end_time   = $curr_end_time->add($appointment_duration);

    }while($curr_end_time <= $end_time);    

    echo "<br/>";
}