Jump to content

[SOLVED] countdown to action


jakebur01

Recommended Posts

I am trying to countdown to an action. I have a datetime in my mysql database for starttime and one for end time. I have searched through scripts in google but i have not found anything.

 

Some thing like:

        if (nowtime>=starttime)
        {
      display code
        }
   else
    {
   show countdown to start time

  compare nowtime with starttime in mysql database using ajax

   when it is start time then display code
    
   now show countdown to end time
      
    check for end time using ajax, when end time is reached then reload page


     }

Link to comment
Share on other sites

correction

 

 if (nowtime>=starttime)
        {
      display code

now show countdown to end time
      
    check for end time using ajax, when end time is reached then header redirect to another page
        }
   else
    {
   show countdown to start time

  compare nowtime with starttime in mysql database using ajax

   when it is start time then display code
    
   now show countdown to end time
      
    check for end time using ajax, when end time is reached then header redirect to another page


     }

 

Link to comment
Share on other sites

be more clear what you want to do

 

//edit

 

if i understand corectly you have a target time in DB and you want to check if current time is lower than that value.

if yes display a countdown else a submit page

*at what game are you working?  ;D* - pm me if you dont wont post here the name ^_^

 

i am wrong ?

if no you already have the code in your post

check if time is higher then display a javascript countdown (dont try a php one,or your server will be dead real quick).

 

Link to comment
Share on other sites

Ok. I have a chat that members will schedule a certain times, the chats will last one hour. On the chat page I need for it to check to see if it is chat time yet... if not then it needs to show a javascript timer counting down (which i have already)... meanwhile i need it checking using ajax to see if its chat time.

 

Then once chat time is reached I need it to count down until the chat ends.. checking with ajax to see if the chat has ended yet, if it has I will redirect the user to another page.

 

The mysql database table is storing the start time and end time in "datetime" columns.

Link to comment
Share on other sites

<?php

$meetingnumber=$_GET["meeting"];

$db = mysql_connect("", "", "");
mysql_select_db("", $db);

$result = mysql_query("SELECT * FROM life_meeting WHERE MeetingId = '$meetingnumber'", $db);

while ($myrow = mysql_fetch_array($result))
{
$meetingstarttime=$myrow["Starttime"];
$meetingendtime=$myrow["Endtime"];
$meetingtype=$myrow["Type"];
$meetingclub=$myrow["Club"];
$meetingtopic=$myrow["Topic"];
$meetingusername=$myrow["Username"];
}

function reformatDate($datetime) {
// I am not sure how to bust up this date time---- all of this below is just guessing with the datetime
list($year, $month, $day, $hour, $min, $sec) = split( '[: -]',
$datetime);
return "$year-$month-$day at $hour:$min";
}

reformatDate($meetingstarttime);

$targetYear  = $year;
$targetMonth = $month;
$targetDay   = $day;
$targetHour  = $hour;
$targetMinute= $min;
$targetSecond= $sec;

$dateFormat = "Y-m-d H:i:s";

$targetDate = mktime($targetHour,$targetMinute,$targetSecond,$targetMonth,$targetDay,$targetYear);
$actualDate = time();

$secondsDiff = $targetDate - $actualDate;

$remainingDay     = floor($secondsDiff/60/60/24);
$remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));

$targetDateDisplay = date($dateFormat,$targetDate);
$actualDateDisplay = date($dateFormat,$actualDate);

?>
<script type="text/javascript">
  var days = <?php echo $remainingDay; ?>  
  var hours = <?php echo $remainingHour; ?>  
  var minutes = <?php echo $remainingMinutes; ?>  
  var seconds = <?php echo $remainingSeconds; ?>  

function setCountDown ()
{
  seconds--;
  if (seconds < 0){
      minutes--;
      seconds = 59
  }
  if (minutes < 0){
      hours--;
      minutes = 59
  }
  if (hours < 0){
      days--;
      hours = 23
  }
  document.getElementById("remain").innerHTML = days+" days, "+hours+" hours, "+minutes+" minutes, "+seconds+" seconds";
  setTimeout ( "setCountDown()", 1000 );
}

</script>
<body onLoad="setCountDown();">

  

<?php

if($nowtime<=$meeting starttime)
{

//countdown and check for starttime
?>

<div id="content">
        <table class="countTable">
          
           <tr><th colspan="2" id="remain"><?php echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?></th></tr>
          
       </table>
    </div>
<?php
/* check to see if start time yet right here*/


}
else
{
//display chat
$chat->printChat();

/* check for endtime right here and redirect to chatend.php when time has been reached. */
}
?>

Link to comment
Share on other sites

dont use that kind of dates instead use unix time stamp

with this kind of predefined dates is quite hard to work,with unix time stamp is easy to work cause is just in seconds.it is not readeable by normal user,but you can convert it how you want :)

 

<?php
$meetingnumber=$_GET["meeting"];

$db = mysql_connect("", "", "");
mysql_select_db("", $db);

$result = mysql_query("SELECT * FROM life_meeting WHERE MeetingId = '$meetingnumber'", $db);

while ($myrow = mysql_fetch_array($result))
{
$meetingstarttime=$myrow["Starttime"];
$meetingendtime=$myrow["Endtime"];
$meetingtype=$myrow["Type"];
$meetingclub=$myrow["Club"];
$meetingtopic=$myrow["Topic"];
$meetingusername=$myrow["Username"];
}
$meetingstarttime=1218973304; //just a test - delete this line for using database values

$te = date("Y-m-d H:i:s", $meetingstarttime);
$secondsDiff = $meetingstarttime - time();

if ( time() > $meetingendtime) {
echo "Chat closed";
}else
if ( time() <= $meetingstarttime)
{
?>

<div id="content">
        <table class="countTable">
          
           <tr><th colspan="2" id="remain"><?=$te;?></th>
<script type="text/javascript">
v=new Date();
var remain=document.getElementById('remain');
function tremain(){
n=new Date();
s=<?=$secondsDiff;?>-Math.round((n.getTime()-v.getTime())/1000.);
m=0;
h=0;
if(s<0){
remain.innerHTML='Chat Time ';document.location=document.location;
}else{
if(s>59){
m=Math.floor(s/60);
s=s-m*60
}
if(m>59){
h=Math.floor(m/60);
m=m-h*60
} 
if(s<10){
s="0"+s
}
if(m<10){
m="0"+m
}
remain.innerHTML=" "+h+"h "+m+"m "+s+'s';document.title=h+':'+m+':'+s+' code provided by Minase. Really please keep copyright.';
window.setTimeout("tremain();",999);
}
}
tremain();
</script>
	   </tr>
          
       </table>
    </div>
<?php
}
else
{
$chat->printChat();
}
?>

 

//edited the code forget an else thing :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.