简体   繁体   中英

How do I compare a time saved in database with the current time?

I want to compare two times (H:M:S)...

Here's the thing... I want to compare a time saved in a table on my database to the current time that PHP returns to me with date() . However, no comparisons are being made as the session variables ( ClassName , ClassStartTime and ClassBlock ) return me an undefined index (I believe that is because they are not saving anything so I'm trying to access something that doesn't exist).

How do I compare the times?

NOTE: The time in my database is being saved as data-type TIME, that's why I'm not performing a strtotime() on the variable today_time . Perhaps, that may be my mistake...

<?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'];
                }
        } 

假设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 . "'"

I believe I found an answer... Thanks to @csb I simulated the query using MySQL function TIME, to extract whatever string that was formatted as a time variable to be returned as time...

Since I'm testing with time, all I'm doing right now is filling up certain tables so more information can be returned. I'll keep you updated with the results, tomorrow.

NOTE: I've found another problem... Certain variables inside the session array DO NOT CHANGE even if the query didn't run or returned another result...

<?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']; ?>">

Here's an update of the code, I'll update tomorrow showing the results. Thanks again @csb and @Lilthilion for your help and tips.

I finally got it! All I needed to do was to transform the time and the query ran, the result i shown below

The result of the query, changing with the time

Now, all that lasts to be done is to get the students list for the form. Thanks a lot, everybody.

Do you have to compare it with PHP time? What about the database time?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM