Display new data greater than 5 minutes with php
To display new data greater than 5 minutes with PHP, you'll need to use a combination of PHP and JavaScript. Here's a basic example:
PHP Code
Create a PHP script that retrieves the data from your database and checks if it's newer than 5 minutes. You can use the time()
function to get the current timestamp and compare it with the timestamp of the data in your database.
<?php
// Connect to your database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Retrieve data from database
$query = "SELECT * FROM your_table";
$result = mysqli_query($conn, $query);
// Check if there are any results
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
// Get the timestamp of the data
$timestamp = $row['timestamp'];
// Check if the data is newer than 5 minutes
if (time() - $timestamp < 300) { // 300 seconds = 5 minutes
// Display the data
echo $row['data'];
}
}
} else {
echo "No data found";
}
// Close the database connection
mysqli_close($conn);
?>
JavaScript Code
Create a JavaScript function that will be called every 5 minutes to retrieve the new data from the PHP script. You can use the setInterval()
function to schedule the function to run at regular intervals.
<script>
function getNewData() {
// Make an AJAX request to the PHP script
$.ajax({
type: "GET",
url: "your_php_script.php",
dataType: "html",
success: function(data) {
// Display the new data
$("#new_data").html(data);
}
});
}
// Call the function every 5 minutes
setInterval(getNewData, 300000); // 300000 milliseconds = 5 minutes
// Create a div to display the new data
<div id="new_data"></div>
</script>
HTML Code
Add a div to your HTML page where you want to display the new data.
<div id="new_data"></div>
How it works
- The PHP script retrieves the data from your database and checks if it's newer than 5 minutes. If it is, it displays the data.
- The JavaScript function makes an AJAX request to the PHP script every 5 minutes using the
setInterval()
function. - The PHP script returns the new data to the JavaScript function, which displays it in the div with the id "new_data".
Note that this is a basic example and you may need to modify it to fit your specific requirements. Additionally, you should ensure that your PHP script is secure and protected from SQL injection attacks.