我想每分钟检查一次当前时间,看它是否与任务的开始时间匹配

时间:2019-03-25 23:11:53

标签: php

除循环外,所有代码均有效。我如何每分钟循环播放我的if语句?

当时间与任务的开始时间匹配并且可以工作时,我尝试刷新页面,但是在下一分钟它不会检查。

$todoConn = mysqli_connect($server, $username, $password, $dbname);

$timeRn = date('h:i A');

echo $timeRn; // This is just a test to see the current time.
$select_query="SELECT * FROM wtdn"; 
$sqlResult=mysqli_query($todoConn,$select_query);

while($row=mysqli_fetch_array($sqlResult)) {

    if ($timeRn == $row['time']) {
        echo "<script>
                    Notification.requestPermission();
                    new Notification('".$row['task_text']."');
                </script>"; // This is just a test
    }

    sleep(60); // Sleep for 60 sec
}

如果当前时间与任务开始时间匹配,则应发送通知。

1 个答案:

答案 0 :(得分:0)

  

首先,这不是性能最高的解决方案,但   它是使用最少量的ajax的最简单解决方案。

您将需要php,html和ajax来实现。

start.php:

<html>
<title>Monitoring task...</title>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
</head>
<body>
<div id="my_header" style="background-color:lightblue">
<p>div header here</p>
</div>

The rest of your html / php page is here...
</body>
<script>
function loadlink(){
    $('#my_header').load('script_task.php',function () {
         $(this).unwrap();
    });
}

loadlink(); // This will run on page load
setInterval(function(){
    loadlink() 

}, 2000); /* this will run after every 2 seconds - change to 60 seconds (60000) */
</script>

script_task.php

<?php 

/* here the code to get the time tasks from the database */

$task_time_from_db = 1622689200; //exemple - Timestamp generated for Saturday, 30-Mar-2019 00:00:00 GMT+0000 using https://www.timestampconvert.com/

$current_time = time();

echo "<pre>This text is inside the script_task.php where the php time() function is updating: <h1>" . gmdate('m-d h:i:s \G\M\T', time()) . "</h1><br>";
echo "*** Warning: This solution is not performative. Depending on what you put in your pscript_task.php the server may get slow! ***<br>";
echo "Current time: " . $current_time . "<br>";
echo "Task time:    " . $task_time_from_db . "<br>";

if($current_time >= $task_time_from_db){
    echo "Macth! Run your code here!";
}
?>