How can PHP be used to fetch and display specific text from a database based on a time calculation?

To fetch and display specific text from a database based on a time calculation in PHP, you can first query the database to retrieve the relevant data based on the time condition. Then, you can use PHP to calculate the time and display the specific text accordingly.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Calculate the current time
$current_time = date("H:i:s");

// Query the database to fetch specific text based on time condition
$sql = "SELECT text FROM table WHERE start_time < '$current_time' AND end_time > '$current_time'";
$result = $conn->query($sql);

// Display the fetched text
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row["text"];
    }
} else {
    echo "No text available at this time.";
}

// Close the database connection
$conn->close();
?>