Hướng dẫn timer in php w3schools

How TO - JavaScript Countdown Timer


Learn how to create a countdown timer with JavaScript.


Try it Yourself »


Creating a Countdown Timer

Example

<!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2024 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>

Try it Yourself »

Tip: Learn more about the window.setInterval() method in our JavaScript Reference.



❮ PHP Misc Reference

Example

Delay execution of the current script for 3 seconds:

<?php
echo date('h:i:s') . "<br>";

//sleep for 3 seconds
sleep(3);

//start again
echo date('h:i:s');
?>

Try it Yourself »


Definition and Usage

The sleep() function delays execution of the current script for a specified number of seconds.

Note: This function throws an error if the specified number of seconds is negative.


Syntax

Parameter Values

ParameterDescription
seconds Required. Specifies the number of seconds to delay the script

Technical Details

Return Value:Returns 0 on success, or FALSE on error.

This function returns a non-zero value if the call was interrupted by a signal. On Windows, this value will always be 192, which is the value of the WAIT_IO_COMPLETION constant within the Windows API. On other platforms, the return value will be the number of seconds left to sleep.

PHP Version:4+
Changelog:Before PHP 5.3.4, this function always returns NULL when sleep has occurred on Windows.

❮ PHP Misc Reference


PHP program to create a Simple Calculator:

The below program is to create a Simple Calculator in PHP.
Example

<!DOCTYPE html>
<html>
<body>
 
<?php
ini_set('display_errors',0);
 
if( isset( $_REQUEST['calculate'] ))
{
$operator=$_REQUEST['operator'];
$n1 = $_REQUEST['first_value'];
$n2 = $_REQUEST['second_value'];
 
if($operator=="+")
{
$res= $n1+$n2;
}
if($operator=="-")
{
$res= $n1-$n2;
}
if($operator=="*")
{
$res =$n1*$n2;
}
if($operator=="/")
{
$res= $n1/$n2;
}
 
if($_REQUEST['first_value']==NULL || $_REQUEST['second_value']==NULL)
{
echo "<script language=javascript> alert(\"Please Enter Correct values.\");</script>";
}
}
?>
 
<form>
<table style="border:groove #00FF99">
 
<tr>
<td style="background-color:turquoise; color:black; font-family:'Times New Roman'">Enter Number</td>
<td colspan="1">
<input name="first_value" type="text" style="color:red"/></td>
</tr>
 
<tr>
<td style="color:red; font-family:'Times New Roman'">Select Operator</td>
<td>
<select name="operator" style="width: 63px">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select></td>
</tr>
 
<tr>
<td style="background-color:turquoise; color:black; font-family:'Times New Roman'">Enter Number</td>
<td class="auto-style5">
<input name="second_value" type="text"  style="color:red"/></td> 
</tr>
 
<tr>
<td></td>
<td><input type="submit" name="calculate" value="Calculate" style="color:wheat;background-color:rosybrown" /></td>	 
</tr>
 
<tr>
<td style="background-color:turquoise;color:black">Output = </td>
<td style="color:darkblue"><?php echo $res;?></td>
</tr>	
 
</table>
</form>
 
</body>
</html>