Forum Moderators: coopster

Message Too Old, No Replies

A clock that keeps counting time

         

dbarasuk

4:57 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



Hi all.

If i create a variable like:
$today=time();
$f_today=date("l, F j, Y", $today);
$f_time=("H:i:s A", $today);

if i do
echo "Today is $f_today. The time is $_time";

That would output obviously for example "Today is Tuesday, April 1, 2008. The time is 18:55:24 PM"

My question is: How can i make the clock, especially the seconds to keep counting visually the time on the screen. I suspect there is a loop inside that, but don't know how to write it.

Have fun there, and thanks already for your upcoming help.

coopster

4:59 pm on Apr 1, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You will need to use some client-side code like JavaScript to keep the clock moving in your browser.

jezra

5:03 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



You will need to write some client side javascript to handle the constant update of the time. A web search for "javascript clock tutorial" should get you started in the right direction.

dbarasuk

6:04 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



I knew it's possible to do it with Jaascript. So, do you mean PHP has no equivalent solution for this task?

Sometimes it's better to know both.

thanks

coopster

7:18 pm on Apr 1, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure, PHP can do it, but you are going to kill your server as well as your client(s) by forcing them to go back and forth every second to update the browser when you can run it client-side in the browser. Remember, PHP is a server-side technology -- each visitor requests a page by connecting to the Apache server, Apache hands the PHP processor the request and your code kicks in and processes the request, sending back the appropriate response to Apache which in turn sends the data back to the browser and then disconnects. Your browser then renders the display for the user.

If you want a clock that runs in PHP you are going to have send requests back and forth through this process every second! Don't do it! Use the tools available that were made just for this type of feature, in this case that tool is client-side code, JavaScript.

PHP_Chimp

7:26 pm on Apr 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP cant do what you want, as it is a server side language. So it can manipulate all you want before you send your data to the client, but once it is out of the server it can do no more.

As great as php is, it is not a client side language. You could always use ajax to get the magic of php onto your client side ;) (although in this case that is probably more hassle than it is worth)

johnnie

1:02 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do this with PHP, but it would be horrible. It would require some sort of refresh every second, since PHP code is handled server-side. Also, your hosting provider would not be happy with this, since you would put a lot of unnecessary load on his or her server. I would go with a javascript.

vincevincevince

1:11 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you do want to do it with PHP, then use an AJAX-like interface to read a timed output feed of the time. It still keeps your server busy, but it's one long sustained connection not many.

coopster

1:17 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It is still many connections, each GET request will open, process and close it's connection. It's the nature of the HTTP protocol.

vincevincevince

1:19 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not if it's written correctly - PHP works just fine doing PUSH when you write your client side handler correctly! Just send out the bytes you want to send, then sleep until it's time for the next line.

coopster

1:27 pm on Apr 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Now I understand ;) I misunderstood this part ...
to read a timed output feed of the time

Still, as has been discussed, it is crazy to work your server like that when the tools designed for tasks like this already exist in the browser. I just wanted to emphasize that point one more time for the OP :)

dbarasuk

3:56 pm on Apr 2, 2008 (gmt 0)

10+ Year Member



thank you. There was a step i did not understand; i.e Apache disconects from the browser when the output is already sent. Thanks a lot.

I turn to JavaScript