Solved

php server time countdown javascript

Posted on 2004-04-08
2
780 Views
Last Modified: 2008-02-26
Hello!

Well basically I have a 30 minutes countdown in Javascript. Currently Javascript
get's the time from user's computer (client side). So if the user's computer clock
is incorrect then the countdown will show wrong time.

What I want is that javascript to get time from SERVER using php. So that javascript
will be based on server's time and not client side user's computer time.

the php also should have some kind of expiry date om when the countdown fully stops

I appreciate any help, thanks!

Below is the script

------------------------

<html>
<head>
<script>
  var interval, m=30, s=0;
  function countdown() {
    if(s==0) {if(m==0) {clearInterval(interval); return;} m-=1; s=60;}
    s-=1;
    document.getElementById("time").innerHTML = m + ":" + (s<10?"0":"") + s;
  }
</script>
</head>
<body onload="interval=setInterval('countdown();',1000);">
<div id="time">30:00</div>
</body>
</html>
0
Comment
Question by:blueshark123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10789713
you will have to use php to generate the javascript code
then use getdate() function to pull out the time from the server.

$now = getdate();
$hours = $now['hours'];
$minutes = $now['minutes'];
$seconds = $now['seconds'];

print "<script language=\"JavaScript\">";
print "var interval,m=$minutes,s=$seconds;
...
print "</script>";

etc
0
 
LVL 5

Accepted Solution

by:
TheClickMaster earned 125 total points
ID: 10798681
<?php
// Configuration ----------------------------
//
// Change this to change the countdown time
$Time["hour"] = 19;
$Time["minute"] = 23;
$Time["second"] = 10;
//-------------------------------------------

//get the current date/time
$current_time = getdate();

// calculate the remaining minutes and seconds
$min = ($Time["hour"] - $current_time["hours"]) * 60;
$min += ($Time["minute"] - $current_time["minutes"]);
$sec = ($Time["second"] - $current_time["seconds"]);

// make sure we get something valid even if we already passed the time limit

// if the number of seconds is below 0
if ($sec < 0)
{
      // we remove 1 minute
      $min --;
      // if there really was 1 minute, we add 60 seconds
      if ($min >= 0)      $sec += 60;
}
// if the number of minutes is below 0 we adjust it to 0
if ($min < 0) $min = 0;


// display the page with the javascript
// we use $min and $ sec to initialize the variables and the display
print "
<html>
<head>
<script>
  var interval, m=".$min.", s=".$sec.";
  function countdown() {
    if(s==0) {if(m==0) {clearInterval(interval); return;} m-=1; s=60;}
    s-=1;
    document.getElementById(\"time\").innerHTML = m + \":\" + (s<10?\"0\":\"\") + s;
  }
</script>
</head>
<body onload=\"interval=setInterval('countdown();',1000);\">
<div id=\"time\">".$min.":".$sec."</div>
</body>
</html>";
?>
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question