/    Sign up×
Community /Pin to ProfileBookmark

Countdown using PHP

Hi,

I have to make a counter which has the following:

Initial value: 0
Target value: 5000
Date which the target should be achieved: 31 Feb 05
Date which the counter starts: 1 Dec 04

So what i’m trying to do is a counter that will start from 0 on the 1stDec and will end with 5000 on the 31stFeb.

However I need to be able to change both the initial and target value and the initial and target date.

Can anyone help me with the code or advise me with a script for this?

Thanks in advance

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@96turnerriDec 02.2004 — completly on the fly so no guarantees

[code=php]
function countdown($int, $end, $start, $finish) {
$start = explode(" ", $start); $finish = explode(" ", $finish);
if(mktime(0, 0, 0, date("d"), date("m"), date("y")) >= mktime(0, 0, 0, $finish[0], $finish[1], $finish[2])) {
return $end;
} elseif(mktime(0, 0, 0, $start[0], $start[1], $start[2]) <= mktime(0, 0, 0, date("d"), date("m"), date("y"))) {
return $int;
} else {
$diff = (mktime(0, 0, 0, $finish[0], $finish[1], $finish[2])-mktime(0, 0, 0, $start[0], $start[1], $start[2]));
$diff2 = $end-$int;
$diff3 = floor($diff/$diff2);
return ((mktime(0, 0, 0, date("d"), date("m"), date("y"))-mktime(0, 0, 0, $start[0], $start[1], $start[2]))/$diff3)+$int;
}
}
[/code]


call by

[code=php]<?php echo countdown(0, 5000, 01 12 04, 31 02 05); ?>
//noticing the slightly altered date formats[/code]
Copy linkTweet thisAlerts:
@cogumeloauthorDec 02.2004 — Hi,

1st of all, thanks a lot for the quick reply!

[code=php]<php function countdown($int, $end, $start, $finish) {
$start = explode(" ", $start); $finish = explode(" ", $finish);
if(mktime(0, 0, 0, date("d"), date("m"), date("y")) >= mktime(0, 0, 0, $finish[0], $finish[1], $finish[2])) {
return $end;
} elseif(mktime(0, 0, 0, $start[0], $start[1], $start[2]) <= mktime(0, 0, 0, date("d"), date("m"), date("y"))) {
return $int;
} else {
$diff = (mktime(0, 0, 0, $finish[0], $finish[1], $finish[2])-mktime(0, 0, 0, $start[0], $start[1], $start[2]));
$diff2 = $end-$int;
$diff3 = floor($diff/$diff2);
return ((mktime(0, 0, 0, date("d"), date("m"), date("y"))-mktime(0, 0, 0, $start[0], $start[1], $start[2]))/$diff3)+$int;
}
}
echo countdown(0, 5000, 01 12 04, 31 02 05); ?>
[/code]


I ran the script this way and it gave me a parse error:

Parse error: parse error, unexpected T_LNUMBER in c:programasapache groupapachehtdocsfooter.php on line 14

Any ideia on what's making this happen?


regards,

cogumelo
Copy linkTweet thisAlerts:
@96turnerriDec 02.2004 — try

[code=php]
<php function countdown($int, $end, $start, $finish) {
$start = explode("-", $start); $finish = explode("-", $finish);
if(mktime(0, 0, 0, date("d"), date("m"), date("y")) >= mktime(0, 0, 0, $finish[0], $finish[1], $finish[2])) {
return $end;
} elseif(mktime(0, 0, 0, $start[0], $start[1], $start[2]) <= mktime(0, 0, 0, date("d"), date("m"), date("y"))) {
return $int;
} else {
$diff = (mktime(0, 0, 0, $finish[0], $finish[1], $finish[2])-mktime(0, 0, 0, $start[0], $start[1], $start[2]));
$diff2 = $end-$int;
$diff3 = floor($diff/$diff2);
return ((mktime(0, 0, 0, date("d"), date("m"), date("y"))-mktime(0, 0, 0, $start[0], $start[1], $start[2]))/$diff3)+$int;
}
}
echo countdown(0, 5000, 01-12-04, 31-02-05); ?>
[/code]


if that still doesnt work i will test it, rather than just coding blind
Copy linkTweet thisAlerts:
@cogumeloauthorDec 02.2004 — hello

i got this error:

Notice: Undefined offset: 1 in c:program filesapache groupapachehtdocsteste.php on line 3

Notice: Undefined offset: 2 in c:program filesapache groupapachehtdocsteste.php on line 3

5000

i'd really appreciate it if you could test it

i can't figure out what's wrong

Thanks in advance
Copy linkTweet thisAlerts:
@JuuitchanDec 02.2004 — The 31st of February?! There can never be more than 29 days in February!
Copy linkTweet thisAlerts:
@JuuitchanDec 02.2004 — [code=php]
echo countdown(0, 5000, 01-12-04, 31-02-05); ?>
[/code]

<i>
</i>I will pretend I am the PHP interpreter.

Octal 1 minus decimal 12 minus octal 4 yields negative fifteen (-15 in decimal). So your third input is the number -15.

You didn't catch this? Sad.
Copy linkTweet thisAlerts:
@cogumeloauthorDec 02.2004 — :eek:

thanks for noticing.

but still doesn't work. any ideia why?


thanx
Copy linkTweet thisAlerts:
@96turnerriDec 02.2004 — ooops, change them to something thats not an operator
Copy linkTweet thisAlerts:
@JuuitchanDec 02.2004 — Why not just put them in quotes to make sure that they are read as strings?
Copy linkTweet thisAlerts:
@cogumeloauthorDec 03.2004 — nope, still doesn't work ?

Parse error: parse error, unexpected T_DNUMBER in c:program filesapache groupapachehtdocsteste.php on line 14

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in c:program filesapache groupapachehtdocsteste.php on line 14 <-- with quotes

what am i doing wrong?

thx
Copy linkTweet thisAlerts:
@Paul_JrDec 03.2004 — The function arguments that contain spaces must be enclosed in quotes:
[code=php]
echo countdown(0, 5000, '01 12 04', '31 02 05');
[/code]

That's what I did, and the error did not occur again. The function doesn't seem to work, though; it always outputs 0.
Copy linkTweet thisAlerts:
@JuuitchanDec 03.2004 — I am going to use the Socratic method here:

First: Why would you try to pass strings without using quotes?

Second: What is the maximum possible number of days in February?

Third: Why make your code so bloody long?

Fourth: How is the PHP function called "time" used?

(I wonder how this "counter" is going to be used...)
Copy linkTweet thisAlerts:
@cogumeloauthorDec 03.2004 — The counter is supposed to start counting from 0 to 5000 within the dates. It will get to 5000 (target value) on the final date

first: i did try it

second: changed

third: show me how to make it shorter


thx
Copy linkTweet thisAlerts:
@96turnerriDec 03.2004 — ok looks like other guys have taken over this, good job cos im real busy and dont like to rush replies (leads to mistooks and stuff)

just tested my code, doesnt work too well (thats what you get for fly-coding, sometimes works spot on sometimes doesnt)

your best bet is to work out the kinda logic you need (if this is less than etc etc) and then use bits from my script and couple of points mentioned by i think Paul and

Jutchan (maybe wrong on their names there)

anyway i will be off for a few days, be back soon
Copy linkTweet thisAlerts:
@cogumeloauthorDec 03.2004 — thx for the help.

i've worked out a solution.
×

Success!

Help @cogumelo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 1.24,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @dert,
tipped: article
amount: 1000 SATS,

tipper: @viney352,
tipped: article
amount: 10 SATS,
)...