如何将数据库中保存的时间与当前时间进行比较?

时间:2018-04-19 02:41:10

标签: php mysqli

我想比较两次(H:M:S)......

这就是......我想将我数据库中表格中保存的时间与PHP返回date()的当前时间进行比较。但是,没有进行比较,因为会话变量(ClassNameClassStartTimeClassBlock)返回了一个未定义的索引(我相信这是因为它们没有保存任何内容,所以我&#39 ;我试图访问不存在的东西。

如何比较时间?

注意:我的数据库中的时间被保存为数据类型TIME,这就是我没有对变量strtotime()执行today_time的原因。也许,这可能是我的错误......

<?php
        // Obtener hora de registro
        date_default_timezone_set('America/Mexico_City'); 

        $today_dW = date('w'); // Get number to know the day of the week. Formatted as {Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6}
        $today_time = date('G:i:s'); // Get time. Formatted as {HH : MM : SS}

        /*

        */
        $class_id_query = "SELECT id_materia, bloque FROM horarios WHERE matricula_prof = '" . $_SESSION['TeacherID'] . "' dia_semana = " . $today_dW . " AND hora_inicio >= " . strtotime($today_time) . ""; 

        // Save query result, if any was found, on var 
        $class_id_result = $connection->query($class_id_query);

        // Check if matching result was found to be posted
        if ($class_id_result->num_rows > 0) 
        {
                // Fetch the associated data if any was found
                while($row = $class_id_result->fetch_assoc()) 
                {
                        $_SESSION['ClassID'] = $row['id_materia'];
                        $_SESSION['ClassStartTime'] = $row['hora_inicio'];
                        $_SESSION['ClassBlock'] = $row['bloque'];
                }
        } 

4 个答案:

答案 0 :(得分:0)

假设列hora_inicio属于TIME类型,则不需要strtotime

$class_id_query = "SELECT id_materia, bloque FROM horarios WHERE matricula_prof = '" . $_SESSION['TeacherID'] . "' dia_semana = " . $today_dW . " AND hora_inicio >= '" . $today_time . "'"

答案 1 :(得分:0)

我相信我找到了答案...... 感谢@csb,我使用MySQL函数TIME模拟查询,提取格式化为时间变量的任何字符串,以便作为时间返回...

由于我经常测试,我现在正在做的就是填写某些表格,以便返回更多信息。我明天会告诉你最新的结果。

注意:我发现了另一个问题......即使查询没有运行或返回另一个结果,会话数组中的某些变量也不会改变...

<?php
    // Get register time
    date_default_timezone_set('America/Mexico_City'); 

    $_SESSION['DayOfWeek'] = date('w'); 
    // Get number to know the day of the week. Formatted as {Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6}

    /*
        This query must return the class ID, to read its information from the classes table                     
    */
    $class_id_query = "SELECT id_materia, bloque, hora_inicio FROM horarios WHERE matricula_prof = '" . $_SESSION['TeacherID'] . "' AND dia_semana = " . $_SESSION['DayOfWeek'] . "AND TIME(hora_inicio) <= TIME(NOW())"; 

    // Save query result, if any was found, on var 
    $class_id_result = $connection->query($class_id_query);

    // Check if matching result was found to be posted
    if ($class_id_result->num_rows > 0) 
    {
        // Fetch the associated data if any was found
        while($row = $class_id_result->fetch_assoc()) 
        {   
            $_SESSION['ClassID'] = $row['id_materia'];
            $_SESSION['ClassStartTime'] = $row['hora_inicio'];
            $_SESSION['ClassBlock'] = $row['bloque'];
        }
    }

    else
    {
        $_SESSION['ClassID'] = "";
        $_SESSION['ClassStartTime'] = "";
        $_SESSION['ClassBlock'] = "";

        echo "Query for class ID cannot be performed";

        exit();
    }

    /*
        Career logo query.
        Logo on ny part of the system
    */
    // Query for the class information
    $career_logo_query = "SELECT nombre, carrera, cuatrimestre FROM materias WHERE id = '" . $_SESSION['ClassID'] . "'";

    // Save query result, if any was found, on var 
    $career_logo_result = $connection->query($career_logo_query);

    // Check if matching result was found to be posted
    if ($career_logo_result->num_rows > 0) 
    {
        // Fetch the associated data if any was found
        while($row = $career_logo_result->fetch_assoc()) 
        {
            $_SESSION['ClassName'] = $row['nombre'];
            $_SESSION['CareerName'] = $row['carrera'];
            $_SESSION['ClassPeriod'] = $row['cuatrimestre'];

            // Show result at index on screen
            echo $_SESSION['CareerName'];
        }
    }
    ?>_logo.png" alt = "Logotipo de <?php echo $_SESSION['CareerName']; ?>">

这是代码的更新,我明天会更新,显示结果。 再次感谢@csb和@Lilthilion的帮助和提示。

答案 2 :(得分:0)

我终于明白了! 我需要做的就是转换时间并运行查询,结果如下所示

The result of the query, changing with the time

现在,所有要做的就是获取表单的学生列表。 非常感谢,大家。

答案 3 :(得分:-1)

你需要将它与PHP时间进行比较吗?数据库时间怎么样?

$class_id_query = "SELECT id_materia, bloque FROM horarios WHERE matricula_prof = '" . $_SESSION['TeacherID'] . "' dia_semana = " . $today_dW . " AND hora_inicio >= NOW()"