sspoke Posted June 7, 2010 Share Posted June 7, 2010 hi guys if you want to help me out a little here. I think i screwed up the math somewhere but i'm trying to create a countdown that starts at 1 hour and keeps going lower and lower until it hits 0 hours 0 minutes 0 seconds then next refresh of the page should reset the countdown back to 1 hour (this is to get people excited when they shop on my shop) <?php $timeleft = 0; $file = fopen("happyhour.txt", "r"); $checkedtime = fgets($file); fclose($file); if(time() - $checkedtime >= 3600) { $file = fopen("happyhour.txt", "w"); $timeleft = time(); fputs($file, $timeleft); fclose($file); } else { $timeleft = 3600 - (time() - $checkedtime); } echo "var hour = ".date("g", $timeleft).";"; echo "var min = ".date("i", $timeleft).";"; echo "var sec = ".date("s", $timeleft).";"; ?> where did i screw up? seems to keep outputting around 7 hours? wth! thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/ Share on other sites More sharing options...
micah1701 Posted June 7, 2010 Share Posted June 7, 2010 are you trying reload the page every hour? if so, you need to use a client-side script such as javascript, not PHP which runs server side. Your script will load once and then output the static text to the browser and nothing else will ever happen unless some script running in the users browser (not on your server) tells the browser to reload the page. Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069074 Share on other sites More sharing options...
sspoke Posted June 7, 2010 Author Share Posted June 7, 2010 no no i'm doing counter count down synchronization with javascript using php. when the javascript counter hits 0 clientside should be same time if not a second off in php so when I'd refresh the page the countdown should start at a hour again serversided in php. Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069078 Share on other sites More sharing options...
sspoke Posted June 7, 2010 Author Share Posted June 7, 2010 so guys what are my options Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069187 Share on other sites More sharing options...
jcbones Posted June 8, 2010 Share Posted June 8, 2010 HLMGTFY There is plenty of tutorials on this subject. Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069276 Share on other sites More sharing options...
sspoke Posted June 8, 2010 Author Share Posted June 8, 2010 excuse me? you think i don't know how to use google? I'm asking for help in fixing my code not rediections to different websites.. I've checked couldn't find what i wanted. Okay lets just forget about javascript okay?? just when i refresh page i want it to update the countdown hours minutes seconds. when the clock hits 0 hours 0 minutes 0 seconds. it should re-save a new timestamp. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069690 Share on other sites More sharing options...
jcbones Posted June 9, 2010 Share Posted June 9, 2010 Sorry, I haven't had a chance to use HLMGTFY in the last few days, so I jumped on it. Example: <?php session_start(); $timestamp = time(); $diff = 3600; if(isset($_SESSION['ts'])) { $slice = ($timestamp - $_SESSION['ts']); $diff = $diff - $slice; } if(!isset($_SESSION['ts']) || $diff > 3600 || $diff < 0) { $diff = 3600; $_SESSION['ts'] = $timestamp; } //Below is demonstration of output. Seconds could be passed to Javascript. $hours = $diff; //$diff holds seconds less than 3600 (1 hour); echo floor($hours / 3600) . ' : '; $minutes = $hours % 3600; echo floor($minutes / 60) . ' : '; $seconds = $minutes % 60; echo floor($seconds); ?> Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069737 Share on other sites More sharing options...
jcbones Posted June 9, 2010 Share Posted June 9, 2010 Since I was being a *(&# I decided to go ahead and make something up for you. It works, but you may want to change the formatting of it. <?php session_start(); $timestamp = time(); $diff = 3600; if(isset($_SESSION['ts'])) { $slice = ($timestamp - $_SESSION['ts']); $diff = $diff - $slice; } if(!isset($_SESSION['ts']) || $diff > 3600 || $diff < 0) { $diff = 3600; $_SESSION['ts'] = $timestamp; } //Below is demonstration of output. Seconds could be passed to Javascript. $diff; //$diff holds seconds less than 3600 (1 hour); $hours = floor($diff / 3600) . ' : '; $diff = $diff % 3600; $minutes = floor($diff / 60) . ' : '; $diff = $diff % 60; $seconds = $diff; ?> <div id="strclock">Clock Here!</div> <script type="text/javascript"> var hour = <?php echo floor($hours); ?>; var min = <?php echo floor($minutes); ?>; var sec = <?php echo floor($seconds); ?> function countdown() { if(sec <= 0 && min > 0) { sec = 59; min -= 1; } else if(min <= 0 && sec <= 0) { min = 0; sec = 0; } else { sec -= 1; } if(min <= 0 && hour > 0) { min = 59; hour -= 1; } var pat = /^[0-9]{1}$/; sec = (pat.test(sec) == true) ? '0'+sec : sec; min = (pat.test(min) == true) ? '0'+min : min; hour = (pat.test(hour) == true) ? '0'+hour : hour; document.getElementById('strclock').innerHTML = hour+":"+min+":"+sec; setTimeout("countdown()",1000); } countdown(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069755 Share on other sites More sharing options...
sspoke Posted June 9, 2010 Author Share Posted June 9, 2010 lol its okay man wow u put alot of work into it there was nothing wrong with my javascript lol its just the php countdown. now it's well session based wont sync up with every person thats why i made it load timestamp from a file and i just wanted someone to correct my code why it gave bad readings Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069765 Share on other sites More sharing options...
sspoke Posted June 9, 2010 Author Share Posted June 9, 2010 solved thanks to you man seems date() fails on 3600 shows it as 7 hours rofl.. but my old server shown it as 1 hour strange but yah.. <?php $timeleft = 0; @$file = fopen("happyhour.txt", "r"); $checkedtime = @fgets($file); @fclose($file); if((time() - $checkedtime > 3600) || time() - $checkedtime < 0) { $file = fopen("happyhour.txt", "w"); $checkedtime = time(); fputs($file, $checkedtime); fclose($file); } $timeleft = 3600 - (time() - $checkedtime); $hours = floor($timeleft/3600); $minutes = floor(($timeleft%3600)/60); $seconds=floor($timeleft%60); echo "var hour = ".$hours.";"; echo "var min = ".$minutes.";"; echo "var sec = ".$seconds.";"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/#findComment-1069778 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.