jakebur01 Posted August 8, 2008 Share Posted August 8, 2008 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 } Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/ Share on other sites More sharing options...
jakebur01 Posted August 8, 2008 Author Share Posted August 8, 2008 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 } Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-611717 Share on other sites More sharing options...
jakebur01 Posted August 11, 2008 Author Share Posted August 11, 2008 bump... Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613490 Share on other sites More sharing options...
Minase Posted August 11, 2008 Share Posted August 11, 2008 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? * - 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). Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613507 Share on other sites More sharing options...
jakebur01 Posted August 11, 2008 Author Share Posted August 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613513 Share on other sites More sharing options...
Minase Posted August 11, 2008 Share Posted August 11, 2008 you dont need ajax for that just php and js give me your js and an example if (something = somethingelse) { } what variable name i should use and explain them it is actually quite easy Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613518 Share on other sites More sharing options...
jakebur01 Posted August 11, 2008 Author Share Posted August 11, 2008 <?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. */ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613556 Share on other sites More sharing options...
Minase Posted August 11, 2008 Share Posted August 11, 2008 why do you complicate soo much/? you are using linux time correct? answer to this question i will optimize the script for you now Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613614 Share on other sites More sharing options...
jakebur01 Posted August 11, 2008 Author Share Posted August 11, 2008 it is on a windows box Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613619 Share on other sites More sharing options...
Minase Posted August 11, 2008 Share Posted August 11, 2008 sorry not linux time.. but *nix time . give me an example what you have in this variables $meetingstarttime=$myrow["Starttime"]; $meetingendtime=$myrow["Endtime"]; Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613620 Share on other sites More sharing options...
jakebur01 Posted August 11, 2008 Author Share Posted August 11, 2008 $meetingstarttime= 2008-08-11 11:00:00 $meetingendtime= 2008-08-11 12:00:00 Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613625 Share on other sites More sharing options...
Minase Posted August 11, 2008 Share Posted August 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613642 Share on other sites More sharing options...
jakebur01 Posted August 11, 2008 Author Share Posted August 11, 2008 do I just set up my mysql column as a timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613660 Share on other sites More sharing options...
Minase Posted August 11, 2008 Share Posted August 11, 2008 not really you can have it as varchar too. use this link for converting current time to unix time http://www.unixtimestamp.com/index.php an example TIME STAMP: 1356106260 DATE: 12 / 21 / 2012 @ 11:11 you need to insert into database that Time Stamp Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613672 Share on other sites More sharing options...
jakebur01 Posted August 11, 2008 Author Share Posted August 11, 2008 Thank you!!! Quote Link to comment https://forums.phpfreaks.com/topic/118803-solved-countdown-to-action/#findComment-613698 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.