elsafraslastra Posted June 8, 2023 Share Posted June 8, 2023 (edited) Hello, I need a code to create a stopwatch (on the server side with start, stop and pause) to be able to be seen by several clients in real time while it is running (only see it without being able to stop, start or pause). Thank you so much. Edited June 8, 2023 by elsafraslastra Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/ Share on other sites More sharing options...
Barand Posted June 8, 2023 Share Posted June 8, 2023 8 minutes ago, elsafraslastra said: with start, stop and pause yet also 8 minutes ago, elsafraslastra said: without being able to stop, start or pause Good luck with that. 1 Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609484 Share on other sites More sharing options...
elsafraslastra Posted June 8, 2023 Author Share Posted June 8, 2023 I mean that it could be two pages, one with the buttons enabled (admin) and the other only showing the elapsed time (client). And transfer from one page to another the value of the variables. Do you think it will be possible? Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609485 Share on other sites More sharing options...
ginerjm Posted June 8, 2023 Share Posted June 8, 2023 If you want the clock to run on the server and show the time on each client that is connected I don't know how PHP can do this for you since it doesn't run on the clients and the clock (as said) is on the server. Getting the picture? If the clock ticks one second by, how does it update each (or any) client? Sounds like a lot of bandwidth for this app - sending time updates every second to every client. How about posting a new topic relative to how to display a clock to a user? Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609486 Share on other sites More sharing options...
ginerjm Posted June 8, 2023 Share Posted June 8, 2023 PS to my previous. No doubt the answer to a new topic (as suggested) will include some JS that will operate on a timer function that uses ajax to keep up with a server-side time. Just a thought that I haven't dug into but it seems logical. Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609487 Share on other sites More sharing options...
kicken Posted June 8, 2023 Share Posted June 8, 2023 I would probably look into using WebSockets for this if possible. Have the clients connect to a WebSocket server and obtain the initial time to display. Each client handle updating the display itself using some local timing, potentially with a periodic sync. Whenever the admin pauses/starts the timer, broadcast that event + the current time to each client so they can pause/start their display and sync it. 1 Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609490 Share on other sites More sharing options...
gordonisnz Posted June 10, 2023 Share Posted June 10, 2023 You could do a PHp script to connect to the server every 30 seconds, and then use a javascript time/clock script to update every second. the PHP connects will be to get the current/latest tier details. Though, you didn't say how long this timer will go on for ? if its a few minutes or less, the above may not be useful. If its longer - it might. Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609525 Share on other sites More sharing options...
Strider64 Posted June 10, 2023 Share Posted June 10, 2023 (edited) On 6/8/2023 at 10:08 AM, elsafraslastra said: Hello, I need a code to create a stopwatch (on the server side with start, stop and pause) to be able to be seen by several clients in real time while it is running (only see it without being able to stop, start or pause). Thank you so much. Game designers/developers do that all the time with a countdown timer - example -> https://www.phototechguru.com/trivia.php. though the server side would be a little tricky if it is automated. Like someone stated websockets might be worth looking into if that is the case? Edited June 10, 2023 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609530 Share on other sites More sharing options...
gizmola Posted June 13, 2023 Share Posted June 13, 2023 On 6/8/2023 at 12:19 PM, elsafraslastra said: I mean that it could be two pages, one with the buttons enabled (admin) and the other only showing the elapsed time (client). And transfer from one page to another the value of the variables. Do you think it will be possible? Yes, and as others have already stated, you need 3 things: Client javascript to run the countdown UI A publish/subscribe feature A persistent bi-directional socket connection between client and server HTTP 1.x protocol, by itself is not designed for persistent connections. When a client sends an HTTP Get request to a server, the server responds with content (usually the HTML of the web page) and the connection to the server is closed. HTTP is not bi-directional nor persistent. In an application like this, you need some way around the way HTTP 1 was designed to work. This is why multiple people have suggested that an application like this should use websockets. Websockets is an alternative protocol designed for persistence. Since Websocket protocol was developed, HTTP 2.x was released, which overlaps to a degree with websockets. The problem with websocket protocol, is that, while it is relatively simple and has both client javascript support, as well as PHP support, from the serverside, you need to run the websocket server in addition to your web server, which adds a degree of complication that can be difficult for someone without a lot of system administration experience to retrofit. The main options for building a PHP based websocket server are: ReactPHP Ratchet Swoole I'd suggest you take a look at these, and perhaps experiment with some of the examples provided. In terms of publish/subscribe, your control client will publish the sync/start/stop/pause, and the other clients subscribe to these events. I will say that this is a non-trivial application, with a lot of design elements to think about. Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609593 Share on other sites More sharing options...
UmarFarooq Posted June 13, 2023 Share Posted June 13, 2023 I can helpThis is the code <?php session_start(); // Initialize session variables if (!isset($_SESSION['start_time'])) { $_SESSION['start_time'] = 0; $_SESSION['pause_time'] = 0; $_SESSION['total_pause_time'] = 0; } // Start the stopwatch if (isset($_POST['start'])) { $_SESSION['start_time'] = time(); $_SESSION['pause_time'] = 0; $_SESSION['total_pause_time'] = 0; } // Pause the stopwatch if (isset($_POST['pause'])) { $_SESSION['pause_time'] = time(); } // Resume the stopwatch if (isset($_POST['resume'])) { $_SESSION['total_pause_time'] += time() - $_SESSION['pause_time']; $_SESSION['pause_time'] = 0; } // Stop the stopwatch if (isset($_POST['stop'])) { $_SESSION['start_time'] = 0; $_SESSION['pause_time'] = 0; $_SESSION['total_pause_time'] = 0; } // Calculate the elapsed time function getElapsedTime() { if ($_SESSION['start_time'] > 0) { $elapsed_time = time() - $_SESSION['start_time'] - $_SESSION['total_pause_time']; return $elapsed_time; } return 0; } ?> <!DOCTYPE html> <html> <head> <title>Stopwatch</title> </head> <body> <h1>Stopwatch</h1> <h2><?php echo gmdate("H:i:s", getElapsedTime()); ?></h2> <form method="POST"> <?php if ($_SESSION['start_time'] == 0) { ?> <button type="submit" name="start">Start</button> <?php } else { ?> <?php if ($_SESSION['pause_time'] == 0) { ?> <button type="submit" name="pause">Pause</button> <?php } else { ?> <button type="submit" name="resume">Resume</button> <?php } ?> <button type="submit" name="stop">Stop</button> <?php } ?> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609595 Share on other sites More sharing options...
gizmola Posted June 19, 2023 Share Posted June 19, 2023 @UmarFarooq Explain how that code in any way helps with what the user asked for. Where did you get this code from? Why didn't you put it in a code block? Where is the explanation of what the code does? Quote Link to comment https://forums.phpfreaks.com/topic/316953-stopwatch-server-side-to-see-from-severals-clients/#findComment-1609822 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.