Page 1 of 1

Very simple countdown clock almost done

Posted: Mon May 24, 2004 2:14 am
by AbrasiveRock
Ok, I wanted a very simple countdown clock. So I went through google and took elements from different scripts and pasted them together. Everything I found had some element I didn't want (like an auto refresh). Now I can't get it to work. If someone can help me fix my errors or even make the code more efficient. Thanks in advance.

The code I have right now is as follows:

Code: Select all

<?
  $day   = 24;     // Day of the countdown
  $month = 5;      // Month of the countdown
  $year  = 2004;   // Year of the countdown
  $hour  = 12;     // Hour of the day
  $min  = 0;     // Minutes of the day
  $sec  = 0;     // seconds of the day
  $event = "next Sceduled chat"; //event

  $calculation = ((mktime ($min,$sec,$hour,0,0,$month,$day,$year) - time(void))/3600);
  $hours = (int)$calculation;
  $days  = (int)($hours/24);
  $mins = (int)$minutes;
  $secs = (int)$seconds;

?>
<?=$days?> days, <?=$hours?> hours, <?=$mins?> minutes, & <?=$secs?> seconds until <?=$event?>
The errors I get are...
Warning: Wrong parameter count for mktime() in http://www.abrasiverock.com/chat.include on line 63
-12561 days, -301484 hours, 0 minutes, & 0 seconds until next Sceduled chat
I'm going to make a wild guess and say the problem is with...

Code: Select all

$calculation = ((mktime ($min,$sec,$hour,0,0,$month,$day,$year) - time(void))/3600);
...but I have no idea how it should be.

Other info you may need to help me is...
PHP Version: 4.2.2
Display Errors: On
Error Level: Not E_ALL
Register Globals: Off

Thanks for any help anyone can give on this. I am a very slow learner, but I will not give up!

Posted: Mon May 24, 2004 2:52 am
by mendingo
OK, rather than just post the answer, which won't help you much, I'll take you through the thought process you should use to find a solution.

First of all, you read the error. You have correctly noted that the error is with the "mktime()"function.

The error says "wrong paramater count". Paramaters are the variables you feed to the function (the things inside the brackets, separated by commas), so the problem appears to be that you have the wrong number of paramaters in the mktime() statement.

You have 8 paramaters, lets find out how many there should be: Look at http://www.php.net, and lookup mktime in the function list: http://uk.php.net/manual/en/function.mktime.php

On this page, we can see that the maximum number of paramaters this function takes is 7 - you have 1 too many.

That is the source of the error, although further investigations shows that you have fed in your paramaters in the wrong order, anyway, so that will also need fixing.

Posted: Mon May 24, 2004 8:16 am
by feyd
...and your calculations for specific days, hours, etc are a bit off too.

Posted: Mon May 24, 2004 2:17 pm
by AbrasiveRock
Cool, thanks for not just giving me the answer and making me learn something. In the long run that is always better.

So let's tackle one problem at a time. Too many eh? Well I don't really need the month or day ones since it's counting down to a daily event. So how about...

Code: Select all

<? 
  $hour  = 12;     // Hour of the day 
  $min  = 0;     // Minutes of the day 
  $sec  = 0;     // seconds of the day 
  $event = "next Sceduled chat"; //event 

  $calculation = ((mktime ($sec,0,0,$hour,$min) - time(void))/3600); 
  $hours = (int)$calculation; 
  $mins = (int)$minutes; 
  $secs = (int)$seconds; 

?> 
<?=$hours?> hours, <?=$mins?> minutes, & <?=$secs?> seconds until <?=$event?>
Is that better? I made a stab at the order from the examples I took this from. It's a total guess though.

Posted: Mon May 24, 2004 2:55 pm
by feyd
your call to mktime parameters are out of order..
the bible wrote:int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])

Posted: Tue May 25, 2004 2:21 am
by mendingo
One of the major advantages PHP has over many other server side languages is its brilliant documentation.

Go to http://www.php.net and type the function you are struggling with into the search box in the top right.

This will tell you everything you need to know about the function, including the paramater order and a description of each paramater.

Have a look at that and it will tell you exactly what paramaters to give.

Posted: Tue May 25, 2004 2:28 am
by mendingo
Also, looking at your code,

Code: Select all

((mktime ($sec,0,0,$hour,$min)
What do the 2 0's represent?

It looks like you've just fed in everything and expect the function to know what you want. It doesn't work that way.

All the function knows is that it will be fed up to 7 numbers. It treats the first one as the hour, the second as the minute etc...

The fact that you've called the first number $sec doesnt't change anything, it's just a name. You could have called it $a, $numberOfSeconds or $greatUncleCaptainMcAlister for all it cares.

Functions take specific paramaters in a specific order, called whatever you like. To find out what paramaters you need to give, you have to look the function up at http://www.php.net

Posted: Tue May 25, 2004 6:09 am
by AbrasiveRock
feyd wrote:your call to mktime parameters are out of order..
the bible wrote:int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])
Ok, so I made a few changes. The current version I have running is...

Code: Select all

<? 
  $hour  = 1;     // Hour of the day
  $min  = 1;     // Minutes of the day
  $sec  = 1;     // seconds of the day
  $event = "next Scheduled chat"; //event

  $calculation = ((mktime ($hour,$min,$sec) - time(void))/3600);
  $hours = (int)$calculation;
  $mins = (int)$minutes;
  $secs = (int)$seconds;

?>
<?=$hours?> hours, <?=$mins?> minutes, & <?=$secs?> seconds until <?=$event?>
I don't really get any errors but the results show me it isn't really working either. Right now it says "-15 hours, 0 minutes, & 0 seconds until next Scheduled chat". This tells me that the hours are way off and it is doing nothing for the min or seconds. Thanks everyone for all the help so far. The PHP site is pretty endless and leaves newbies like me not even knowing where or what to look at.

Posted: Tue May 25, 2004 11:01 am
by feyd
$minutes and $seconds aren't calculated.

SPOILER
























Code: Select all

<?php

$now = time();
//$now = mktime(13);

$then = mktime(12,0,0);

echo date("Y.m.d H:i:s T",$now)."<br />\n";
echo date("Y.m.d H:i:s T",$then)."<br />\n";

if($then < $now)
{
	$then += (24*60*60);
}

$diff = $then - $now;

echo $diff%60;	//	seconds
echo "<br />\n";
$diff = round($diff/60);
echo $diff%60;	//	minutes
echo "<br />\n";
$diff = round($diff/60);
echo $diff;		//	hours
echo "<br />\n";

?>


































.

Posted: Wed May 26, 2004 3:33 am
by AbrasiveRock
You call that a spolier? Dang, I must have a long ways to go still. Mind explaining what the

Code: Select all

$then += (24*60*60);
part does? That is the only part I could not figure out. Again, thanks for everyones help.

Posted: Wed May 26, 2004 7:12 am
by magicrobotmonkey
it adds a day to $then

the "+=" is the same as going $then = $then + (24*60*60)

the (24*60*60) is the number of seconds in a day!
60 sec/min * 60 min/hr *24 hr/day