Jump to content

[SOLVED] Help with PHP countdown script


ILYAS415

Recommended Posts

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

Link to comment
Share on other sites

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>";

?>

Link to comment
Share on other sites

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>';

?>

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.