PHP倒计时时间表

时间:2014-12-16 16:17:08

标签: php

我有这些时间:

08:00
09:00
10:00
11:00
12:00
13:00
14:00
15:00

如何将此时间表变为倒计时?因此,例如,如果时间为06:45,则会说:

1:15 left until 1 (number of the time)

如果时间超过08:00,则会说:

1:00 left until 2 (number of the time)

修改

我试过这个,但出了点问题:

$bellday = date("N");
$time = date('H:i');

define('TIME', $time);

$bell = array
(
    1 => '8:00',
    2 => '9:00',
    3 => '10:00',
    4 => '11:00',
    5 => '12:00',
    6 => '13:00',
    7 => '14:00',
    8 => '15:00'
);
function getTime($time)
{
    $date = strtotime($time);
    $time = abs($date - strtotime(TIME));
    $hours = (int)($time / 3600);
    $min = (int)(($time - $hours * 3600) / 60);

    if($hours == 0)
        $hours = '';
    else
        $hours = $hours.":";

    $bellinfo = "$hours$min left";
}

1 个答案:

答案 0 :(得分:1)

这应该适合你:

<?php

    $times = array("08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00");
    $input = "08:00";

    foreach($times as $k => $v) {
        $temp = new DateTime("$v");
        $time = new DateTime("$input");

        if($v ==$last = end($times))
            echo $input . " is later then any time in the array!";          

        if($time >= $temp) {
            continue;
        } else {
            $left = $time->diff($temp);
            printf('%d hours, %d minutes left until %d (number of the time)', $left->h, $left->i, $k+1);
            break;
        }
    }

?>

输出:

1 hours, 0 minutes left until 2 (number of the time)