Duke
Duke

Reputation: 37060

How to create a function to get time slots in a day in PHP?

I need to get an array of time slots in a day, i.e. 24 hours. Something like:

array ( 
    00:00=>00:00,
    00:05=>00:05,
    00:10=>00:10,
    .................
    21:05=>21:05,
    .....
    23:55=>23:55,
    24:00=>24:00
 )

I want to get this as a function return value with 5 minute intervals. Sorry for my bad English.

Upvotes: 1

Views: 1988

Answers (2)

Linus Kleen
Linus Kleen

Reputation: 34652

No need for a date function:

$result = array();
for ($n = 0; $n < 24 * 60; $n+=5)
{
   $date = sprintf('%02d:%02d', $n / 60, $n % 60);
   $result[$date] = $date;
}

BTW: There's no such thing as 24:00 hours.

Upvotes: 3

Alex Pliutau
Alex Pliutau

Reputation: 21947

$result = array();
for ($i = 0; $i < 24; $i++) {
    for ($j = 0; $j < 60; $j+=5) {
        $time = sprintf('%02d', $i) . ':' . sprintf('%02d', $j);
        $result[$time] = $time;
    }
}

Upvotes: 0

Related Questions