Reputation: 15
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
date("Y-m-d");
date_default_timezone_set('Asia/Kolkata');
$time= date('H:i');
if($time == '11:51')
{
echo mt_rand(0,9);
echo '<br>';
}
?>
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
<?php
echo "Watch the page reload itself in 10 second!";
?>
</body>
</html>
// in this code insted of second i wont to pass time like 12.00 then the code execute in above code after 10 second page refresh and generate number on every 10 sec and i also wont to generate 1 random number on particular time
Upvotes: 0
Views: 1290
Reputation: 385
You can do this in both JavaScript and PHP , JavaScript solution is already above. Here is a PHP solution:
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
date("Y-m-d");
date_default_timezone_set('Asia/Kolkata');
$time= date('H:i');
if($time == '11:51')
{
echo mt_rand(0,9);
echo '<br>';
}
?>
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");
?>
<html>
<head>
</head>
<body>
<?php
echo "Watch the page reload itself in 10 second!";
?>
</body>
</html>
Upvotes: 0
Reputation: 60
PHP is server side, any code there will not get called without a request from the browser, the solution is AJAX. In your case try something like this (you need jQuery for this particular example):
window.setInterval(function(){
$.ajax({
url:"my-file.php",
success:function(data){
//now data is the current time on the server, react accordingly with location.reload(true);
});
}, 10000); // check every 10 seconds (change to any milliseconds you want)
In your my-file.php just echo the server time
Upvotes: 0
Reputation: 6254
You should make your code something like:
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
date("Y-m-d");
date_default_timezone_set('Asia/Kolkata');
$time= date('H:i');
if($time == '11:51')
{
echo mt_rand(0,9);
echo '<br>';
}
?>
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
?>
<html>
<head>
<script type="text/javascript">
setInterval(function() {
window.location.href = '<?php echo $page?>';
},<?php echo (int)$sec ?> * 1000);
</script>
</head>
<body>
<?php
echo "Watch the page reload itself in 10 second!";
?>
</body>
</html>
Upvotes: 0
Reputation: 778
You can you use this little Javascript
snippet to refresh the page.
window.onload = function() {
setInterval(function() {
location.reload(true);
},21 * 1000); //21 is after how much seconds you wanna reload!
};
Upvotes: 1