0

i want to make some changes for my site,but i don't know how; and i will be glad if you could help me with that, if its possible to see really online the seconds counting until zero,not only the time has left and second, if i want that the countdown will start automatically to count again and start from the begining by a loop.

here is the code that I want to insert my changes

<?php
 
//You must call the function session_start() before
//you attempt to work with sessions in PHP!
session_start();
 
//Check to see if our countdown session
//variable has been initialized.
if(!isset($_SESSION['countdown'])){
    //Set the countdown to 120 seconds.
    $_SESSION['countdown'] = 120;
    //Store the timestamp of when the countdown began.
    $_SESSION['time_started'] = time();
}
 
//Get the current timestamp.
$now = time();
 
//Calculate how many seconds have passed since
//the countdown began.
$timeSince = $now - $_SESSION['time_started'];
 
//How many seconds are remaining?
$remainingSeconds = abs($_SESSION['countdown'] - $timeSince);
 
//Print out the countdown.
echo "There are $remainingSeconds seconds remaining.";
 
//Check if the countdown has finished.
if($remainingSeconds < 1){
   //Finished! Do something.
}

​
  • 4
    If you want the countdown to be visible / countdown for the user on your site I'd recommend using jQuery / JS to achieve this instead, as PHP loads once, executes and doesn't display it's data before it's done. – Epodax May 26 '15 at 12:38
  • +1 and if you want to do something at the moment some countdown reaches zero you can use a metatag refresh setting the seconds or an ajax function called by that jquery Epodax is suggesting – javier_domenech May 26 '15 at 12:42
  • I need a countdown by server side,and from what I understand;when I made a js countdown it allways start over when every user open his browser,I need a countdown that display the seconds until zero that every client sees the exact time left – Tomer Kravitz May 26 '15 at 12:48

1 Answers1

0

PHP is a server side scripting language and it stands for Preprocessor Hyper Text (Personal Home Page before that).

What PHP does is not what you want to do with it if you catch my drift.

PHP takes dynamic code e.g. records from a database and generates the HTML that goes with it (often you have to create loops for products etc...)

When PHP is done it sends the response to the browser and is done processing from there.

You want to add a counter that works live on the page itself.

This is a different story.

You'll need to write some JS (JavaScript) on page load that will count down from where the counter was when the page first loaded.

You could make a script in JS that takes a given timestamp and counts down from there.

Each time you load a new page with the JS doing its thing onload will make sure the counter will count down until it hits 0.

I would love to help you further but I'm @ work at this moment so all I can do is point you into the right direction.

What I think you want to do:

  • get timestamp from PHP and insert it into a div
  • load JS on pageload
  • fetch the hidden div timestamp
  • countdown dynamicly from the timestamp using js

And one last thing (seeing as you don't seem to have alot of experience with php / js) - instead of using jQuery, further complicating things for a very small puny task I would look up a plain JS example of what you are trying to do and learn from there.

using jQuery for just this is waaaaay overkill and not needed at all - adding 90kb code for a silly clock that is ;)

SidOfc
  • 4,552
  • 3
  • 27
  • 50