ILYAS415 Posted August 4, 2007 Share Posted August 4, 2007 Hiya, i need help with a php countdown script that is supposed to echo a time to the next reset (which is every 4 hours starting from 12:00 AM). Heres what i got at the moment and im trying to get it right because at the moment its saying i got 9hours left to the next reset. This is wrong because the next reset is always every 4 hours. Heres my code... <?php $gettime= gmdate('h'); if ($gettime > "00" && $gettime < "04"){ $times="4"; }elseif ($gettime > "04" && $gettime < "08"){ $times="8"; }elseif ($gettime > "08" && $gettime < "12"){ $times="12"; }elseif ($gettime > "12" && $gettime < "16"){ $times="16"; }elseif ($gettime > "16" && $gettime < "20"){ $times="20"; }elseif ($gettime > "20" && $gettime < "23"){ $times="0"; } countdown($time, 0); function countdown($time, $minute) { $day= gmdate('d'); echo "$day<br>"; // make a unix timestamp for the given date $the_countdown_date = mktime($day, $hour, $minute, 0); // get current unix timestamp $today = gmdate('h i s'); $difference = $the_countdown_date - $today; if ($difference < 0) $difference = 0; $days_left = floor($difference/60/60/24); $hours_left = floor(($difference - $days_left*60*60*24)/60/60); $minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60); // OUTPUT echo "Next reset in: ".$hours_left."h ".$minutes_left."m"; } ?> I would appreciate it if you can help me with this code. I need it for a txt-based game im making. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/63335-solved-help-with-php-countdown-script/ Share on other sites More sharing options...
Psycho Posted August 4, 2007 Share Posted August 4, 2007 That's way too much work for what you are trying to accomplish. <?php $gethours = intval(gmdate('h')); $getminutes = intval(gmdate('i')); $hours_left = (4-($gethours%4)); if ($getminutes!=0) { $hours_left = ($hours_left-1); $minutes_left = str_pad ((60-$getminutes),2,'0'); } else { $minutes_left = '00'; } echo "Next reset in: ".$hours_left."h ".$minutes_left."m<br><br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/63335-solved-help-with-php-countdown-script/#findComment-315680 Share on other sites More sharing options...
ILYAS415 Posted August 4, 2007 Author Share Posted August 4, 2007 Thanks! it worked! if its not too much trouble for you could you also add in seconds cause i hav no idea how to add that in. Or can you send me a link to the reference you got that from (if u did that is) Quote Link to comment https://forums.phpfreaks.com/topic/63335-solved-help-with-php-countdown-script/#findComment-315704 Share on other sites More sharing options...
dbillings Posted August 4, 2007 Share Posted August 4, 2007 This will give you seconds. gmdate('i'); PHP.net is one stop shopping. http://us.php.net/manual/en/function.gmdate.php Quote Link to comment https://forums.phpfreaks.com/topic/63335-solved-help-with-php-countdown-script/#findComment-315713 Share on other sites More sharing options...
ILYAS415 Posted August 4, 2007 Author Share Posted August 4, 2007 dont gmdate('i') mean minutes? Quote Link to comment https://forums.phpfreaks.com/topic/63335-solved-help-with-php-countdown-script/#findComment-315716 Share on other sites More sharing options...
Psycho Posted August 5, 2007 Share Posted August 5, 2007 gmdate('s') is seconds. I wrote that off-the-cuff. Here is the code with seconds added and made even more efficient: <?php $hours = intval(gmdate('h')); $minutes = intval(gmdate('i')); $seconds = intval(gmdate('s')); $hours_left = (4 - ($hours % 4)) - (($minutes)?1:0); $minutes_left = ($minutes)?((60 - $minutes)-(($seconds)?1:0)):0; $minutes_left = str_pad ($minutes_left , 2, '0', STR_PAD_LEFT); $seconds_left = ($seconds)?(60 - $seconds):0; $seconds_left = str_pad ($seconds_left , 2, '0', STR_PAD_LEFT); echo 'Next reset in: '.$hours_left.'h '.$minutes_left.'m '.$seconds_left.'s<br><br>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/63335-solved-help-with-php-countdown-script/#findComment-315877 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.