How To Set Timer In Php Page
Create Countdown Timer in PHP
- Create a Static Countdown Timer in PHP
- Utilize
TimeCircles.js
to Create a Dynamic Inaugural Timer in PHP
In this article, we will innovate a few methods to create a countdown timer in PHP. We can create a static countdown timer using only PHP. To brand a dynamic countdown timer, we volition have to use JavaScript.
Create a Static Countdown Timer in PHP
Nosotros tin create a static countdown timer using PHP. The timer will only become updated when we refresh the page. Otherwise, the timer volition stay the same. PHP runs on the server, and the page should be refreshed when new information is needed from the server. The inaugural timer works perfectly, simply nosotros have to refresh the web page whenever we see progress.
We will use the PHP office time()
to get the current time. Nosotros can find the departure between the target appointment and the current time, and format them in days, hours, minutes, and seconds. We will utilize the floor()
function while converting the time left into the other units. The role will round the event down to the nearest integer. As a result, the newly calculated unit of measurement of time will be authentic. The time()
office gives the current time in seconds since January 1 1970 00:00:00 GMT. We can use EpochConverter to convert the current date and time to the timestamps and vice versa.
For example, selection a target date and use the EpochConverter to convert the date into Epoch timestamps. Then, create a variable $waiting_day
and assign the timestamp to it. Then detect the departure between $waiting_day
and the current time and shop the event in a variable $time_left
. Now, calculate the total number of days dividing $time_left
with the total number of seconds in a day, 60*60*60
. Use the floor()
function while converting. Store the result in the $days
variable. Next, update the $time_left
variable to find the remaining hours in terms of seconds. For this, find the remainder while dividing $time_left
by sixty*60*threescore
. Apply the %
operator to find the remainder. Similarly, find the remaining hours, minutes, and seconds. To find the hours, divide $time_left
by 60*60
, and to find minutes, divide past threescore
. The remaining timestamps will be in seconds. Finally, print all the variables where days, hours, minutes, and seconds are stored.
Thus, we created a static countdown timer using PHP.
Example Code:
<?php $waiting_day = 1637210196; $time_left = $waiting_day - time(); $days = floor($time_left / (threescore*60*24)); $time_left %= (60 * 60 * 24); $hours = flooring($time_left / (60 * 60)); $time_left %= (60 * 60); $min = floor($time_left / lx); $time_left %= 60; $sec = $time_left; repeat "Remaing time: $days days and $hours hours and $min min and $sec sec left"; ?>
Output:
Remaing time: 17 days and 23 hours and 51 min and 26 sec left
Apply TimeCircles.js
to Create a Dynamic Countdown Timer in PHP
We can create a dynamic countdown timer using the TimeCircles.js
library in PHP. The countdown timer will update itself without refreshing the webpage. TimeCircles is a jQuery plugin that helps to create a countdown timer with ease. Nosotros can use CDN to include the TimeCircles.js
and jQuery in our script. The script tags are as follows.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.six.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="bearding" referrerpolicy="no-referrer"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/timecircles/i.5.3/TimeCircles.min.js" integrity="sha512-FofOhk0jW4BYQ6CFM9iJutqL2qLk6hjZ9YrS2/OnkqkD5V4HFnhTNIFSAhzP3x//AD5OzVMO8dayImv06fq0jA==" crossorigin="bearding" referrerpolicy="no-referrer"></script>
For example, create a div
with a data-date
attribute. Write PHP as the value of the attribute. Utilize the repeat
part to display the target date 2019-02-12 07:25:fourteen
. Next, set the id
of the div
as cd_timer
. Next, open the script
tag and write some jQuery. Write a role and select the id cd_timer
and call the TimeCircles()
office.
When we run the webpage from localhost, we can see a countdown timer showing the remaining days, hours, minutes, and seconds. In this way, we tin create a dynamic countdown timer using TimeCircle.js
in PHP.
Example Code:
<div data-date="<?php repeat '2019-02-12 07:25:14' ; ?>" id="cd_timer"></div> <script> $(role () { $("#cd_timer").TimeCircles(); }); </script>
Write for united states of america
DelftStack articles are written past software geeks similar you. If you too would like to contribute to DelftStack by writing paid manufactures, you lot tin can check the write for us folio.
Related Article - PHP Timer
How To Set Timer In Php Page,
Source: https://www.delftstack.com/howto/php/php-countdown-timer/
Posted by: harrismith1999.blogspot.com
0 Response to "How To Set Timer In Php Page"
Post a Comment