How to make a countdown using PHP

I am creating a website for the wedding of the brothers. And while everything is going well. However, he wants a countdown to the wedding on the main page;

Time before the wedding: X months, X days, X hours.

I would prefer to do this with php, but would be open to other suggestions.

if you can help me with ideas for coding or just point me to the relevant material, that would be helpful.

The wedding will take place on Saturday July 30th.

+5
source share
8 answers

If you want your counter to appear only when the page is refreshed and to be static after the page loads, then PHP will be fine.

If you need a countdown to refresh when the page is displayed, you will need to use JavaScript.

Personally, I would go for something already implemented, such as a small script .

+5
source

For a static countdown:

//A: RECORDS TODAY Date And Time $today = time(); //B: RECORDS Date And Time OF YOUR EVENT $event = mktime(0,0,0,12,25,2006); //C: COMPUTES THE DAYS UNTIL THE EVENT. $countdown = round(($event - $today)/86400); //D: DISPLAYS COUNTDOWN UNTIL EVENT echo "$countown days until Christmas"; ?> 
+2
source

The following is a snippet of code that I copied from W3Schools , adding my PHP code to get a countdown to the "timestamp and timestamp" now.

You will see that I commented on the original JavaScript code and replaced it with PHP code in two places, so it’s clear to find out what is the difference between the two parameters.

Basically, if you think "server time" is more reliable in your case, you can take the PHP approach.

 <!DOCTYPE HTML> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> p { text-align: center; font-size: 60px; margin-top: 0px; } </style> </head> <body> <p id="demo"></p> <script> // Set the date we're counting down to // 1. JavaScript // var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime(); // 2. PHP var countDownDate = <?php echo strtotime('Sep 5, 2018 15:37:25') ?> * 1000; var now = <?php echo time() ?> * 1000; // Update the count down every 1 second var x = setInterval(function() { // Get todays date and time // 1. JavaScript // var now = new Date().getTime(); // 2. PHP now = now + 1000; // Find the distance between now an 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); // Output the result in an element with id="demo" document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; // If the count down is over, write some text if (distance < 0) { clearInterval(x); document.getElementById("demo").innerHTML = "EXPIRED"; } }, 1000); </script> </body> </html> 
+2
source

I would not use php (serverside) for this. Because you need to refresh your page every time to see the score. It is preferable to use javascript (clientside) for this, more specific jquery (javascript framework). And find the jquery plugin, for example: http://keith-wood.name/countdown.html

0
source

If you want something in real time, you will need to use client-side scripts, namely JavaScript.

You can do this in PHP, but it will not “animate”:

 $wedding = strtotime("2011-07-01 12:00:00+0400"); // or whenever the wedding is $secondsLeft = $wedding - time(); $days = floor($secondsLeft / 60*60*24); $hours = floor(($secondsLeft - $days*60*60*24) / 60*60); echo "$days days and $hours hours left"; 

Within a few months, you could add a few mathematical calculations, but this becomes more useless because a month is not a fixed amount of time.

Another way would be to use the date() function to highlight individual elements (hour, minute, second, day, month, etc.) and use the rollover math, but this is very worrying about the “good effect”.

Let me know if you want to use the JavaScript example. Don't worry about jQuery - this is the canon for killing a mosquito :)

0
source

try it

 <?php $target = mktime(0, 0, 0, 9, 25, 2011) ;//set marriage date $today = time () ; $difference =($target-$today) ; $month =date('m',$difference) ; $days =date('d',$difference) ; $hours =date('h',$difference) ; print $month." month".$days." days".$hours."hours left"; ?> 
0
source

try it

 $wedding = strtotime("2014-02-20 12:00:00"); // or whenever the wedding is $current=strtotime('now'); $diffference =$wedding-$current; $days=floor($diffference / (60*60*24)); echo "$days days left"; 
0
source

How about such a simple solution? Example PHP Countdown Timer Example PHP Countdown Timer

-2
source

Source: https://habr.com/ru/post/1333025/


All Articles