从mysql获取日期和时间到输出到html表

时间:2013-09-13 20:16:33

标签: php html mysql

我一直在研究这个问题已经有一段时间了,无法理解。

我想要做的是运行一个函数,该函数将调用特定日期的所有记录并将其输出到html表中。

我可以通过在循环内运行查询来实现,但我想学习如何使用数组来实现这一目标。

function getAll(){
    include('inc/connect.php');
    $a = $data->query("SELECT * FROM bookings WHERE active = 'true'");
    $args = array();

    while($b = $a->fetch_array()){
        $t1 = strtotime($b["endtime"]);
        $t2 = strtotime($b["starttime"]);
        $diff = $t1 - $t2;
        $hours = $diff / ( 60 * 60 );
        $args[] = array('start' => $b["starttime"],
                        'end' => $b["endtime"],
                        'span' => $hours
                    );
    }
    return $args;
}

上面是我运行以获取所有并返回数组的函数,但我对数组很新,并且不知道如何针对下面运行它们。

$interval = 1800; // Interval in seconds

$date_first     = "02-09-2013 00:00:00";
$date_second    = "03-09-2013 00:00:00";

$time_first     = strtotime($date_first);
$time_second    = strtotime($date_second);

$go = getAll();

for ($i = $time_first; $i <= $time_second; $i += $interval){
    $timeOut = date('d-m-Y H:i', strtotime("+30 minutes", $i));

    //////I WOULD LIKE TO RUN THE ARRAY CALLED BY $go HERE TO SEE IF start == $timeOut/////
}

我试着将以下结果作为td colspan表示到html表上以表示时间。在此先感谢您的帮助。

enter image description here

1 个答案:

答案 0 :(得分:0)

非常确定这不是处理此数据的正确方法,但如果您只是想使用数组,

试试这个:

$interval = 1800; // Interval in seconds

$date_first     = "02-09-2013 00:00:00";
$date_second    = "03-09-2013 00:00:00";

$time_first     = strtotime($date_first);
$time_second    = strtotime($date_second);

$go = getAll();


for ($i = $time_first; $i <= $time_second; $i += $interval){
    $timeOut = date('d-m-Y H:i', strtotime("+30 minutes", $i));

    //////I WOULD LIKE TO RUN THE ARRAY CALLED BY $go HERE TO SEE IF start == $timeOut/////

    // To process all entries in an array use the FOREACH verb
    // foreach entry in the array it will return the array key and the value associated with that key
    // as 2 new variables. As the value is another array I have called it $innerArray

    foreach ( $go as $key => $innerArray ) {

        if ( $innerArray['start'] == $timeout ) {

            $msg = 'timeout = ' . $timeout;
            $msg .= ' $go key = ' . $key;
            $msg .= ' innerArrays[start] = ' . innerArrays['start'];
            $msg .= ' innerArrays[end] = ' . innerArrays['end'];
            $msg .= ' innerArrays[span] = ' . innerArrays['span'];
            // not sure it you are using the CLI or Web Server so feel free to change this to something more simple
            $msg .= strtoupper(PHP_SAPI) == 'CLI' ? PHP_EOL : '<br>';

            echo $msg;
        }
    }
}