Creating a time interval that goes into array

时间:2015-06-15 14:30:46

标签: php arrays loops time

I trying to create something like this

Our time starts at 15 minutes which will be 15:00 our interval will be X that will be given in a variable but assume for now 0:30 seconds so I will get 14:30,14:00... up to 0:00. I would like to have the result in an array which I can later refere to.

Nothing comes to my mind how to achieve this. Thanks for any tips.

$interval = 30; //seconds

For now I use this array ( int = 15s ) but I want it to me somehow more dynamic when I want set a interval.

$intervals = array(
    "15:00",
    "14:45",
    "14:30",
    "14:15",
    "14:00",
    "13:45",
    "13:30",
    "13:15",
    "13:00",
    "12:45",
    "12:30",
    "12:15",
    "12:00",
    "11:45",
    "11:30",
    "11:15",
    "11:00",
    "10:45",
    "10:30",
    "10:15",
    "10:00",
    "09:45",
    "09:30",
    "09:15",
    "09:00",
    "08:45",
    "08:30",
    "08:15",
    "08:00",
    "07:45",
    "07:30",
    "07:15",
    "07:00",
    "06:45",
    "06:30",
    "06:15",
    "06:00",
    "05:45",
    "05:30",
    "05:15",
    "05:00",
    "04:45",
    "04:30",
    "04:15",
    "04:00",
    "03:45",
    "03:30",
    "03:15",
    "03:00",
    "02:45",
    "02:30",
    "02:15",
    "02:00",
    "01:45",
    "01:30",
    "01:15",
    "01:00",
    "00:45",
    "00:30",
    "00:15",
    "00:00"
);

3 个答案:

答案 0 :(得分:0)

$start = strtotime('00:15:00');        // start time
$end = strtotime('00:00');             // end time (must be less)
$interval = 30;                        // in seconds
$arr = array();

do {
    $arr[] = $i = date("i:s", $start);
    $start = strtotime("00:" . $i . " -". $interval ." second");
} while($start >= $end);

print_r($arr);

答案 1 :(得分:0)

You could try something, like this:

function generateTimeArray($start, $end, $interval) {
    $t = strtotime($end) - $interval;
    $results = array(date("H:i:s", strtotime($end)));
    while ($t >= strtotime($start)) {
        $results[] = date("H:i:s", $t);
        $t -= $interval;
    }
    return $results;
}

$results = generateTimeArray("00:00:00", "00:15:00", 15);
print_r($results);

答案 2 :(得分:0)

$interval = 15; //seconds
$time = 15; //minutes
$sec_time = $time*(60/$interval); //We are converting 15 minutes to seconds. 

$in = array();
$in[0]=$time.":00";
$count = 0;

while ($sec_time > 0){
//This will only work alright if 60 can be divided by $interval, else, you'll get floats
   $in[count]=$sec_time%60.":".$sec_time/60;
   $count++;
   $sec_time = $sec_time-$interval;
}
print_r($in);