Click to See Complete Forum and Search --> : please help, how to change getSeconds() integer value between 0 and 20


sharkblue
04-07-2004, 01:25 AM
Hello!

Basically I have a countdown script which counts down to a specific date. It consists of php and javascript, php is used only to get servers time and not user's computer time.

Currently the output looks like this let's say "58:12", 58 are minutes and 12 are seconds. there are no hours in the output.

---------------------------
Here is what I want

there are 0 to 59 seconds equal to 1 minutes, what I want is only 20 seconds to equal to 1 minutes. So once 20 seconds passes it goes one minute down and so on.... Please tell me how to do that. Below is my php page with scripts

p.s. sorry for my english, not my first language
-------------------------------

<?php
// Setup the expire date/time
$expiredate = "2005-08-30";
$expiretime = "20:58";

// calculate seconds to expire date/time
$exptime = explode(":",$expiretime);
$expdate = explode("-",$expiredate);
$expiretimestamp = mktime($exptime[0],$exptime[1],0,$expdate[1],$expdate[2],$expdate
[0],-1);
$seconds_left = $expiretimestamp - time();

// has expire date/time passed?
$countdown = ($seconds_left >= 0) ? true : false;
?>
<html>
<head>
<title>COUNTDOWN</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<?php
if ($countdown) // if the expire date/time hasn't been reached
{
?>
<script language="JavaScript">
<!--
function showtime() {
setTimeout("showtime();",1000);
sourcedate.setTime(sourcedate.getTime()-1000);
var hh = (sourcedate.getDate()-1)*24 + sourcedate.getHours()-1;
if ( hh < 0 ) {
document.all["clock"].innerText = '';
this.location.href = this.location.href; // Reload the page
}
var mm = sourcedate.getMinutes();
var ss = sourcedate.getSeconds();
if (hh >= 0) {
document.all["clock"].innerText = ((mm < 10) ? "0" : "") + mm + ((ss < 10) ? ":0" : ":") + ss;
}
}

sourcedate = new Date(<?= date("Y,m,d,H,i,s",$seconds_left);?>);
//-->
</script>
<?php
} // end if ($countdown)
?>
</head>

<body>
<?php
if ($countdown) // if the expire date/time hasn't been reached
{
?>
<span name="clock" id="clock" class="top_tbl"></span>
<?php
}
else // Show some other content
{
?>
We have passed the expiredate/time: <?= $expiredate . ' ' . $expiretime; ?>
<?php
}
?>

<script language="JavaScript">
<!--
showtime(); // Init the function
//-->
</script>
</body>
</html>

Pittimann
04-07-2004, 05:06 AM
Hi!

You can achieve that with a tiny modification. Here it is:

<script language="JavaScript" type="text/javascript">
<!--
function showtime() {
setTimeout("showtime();",1000/3);
sourcedate.setTime(sourcedate.getTime()-1000);
var hh = (sourcedate.getDate()-1)*24 + sourcedate.getHours()-1;
if ( hh < 0 ) {
document.all["clock"].innerText = '';
this.location.href = this.location.href; // Reload the page
}
var mm = sourcedate.getMinutes();
var ss = parseInt(sourcedate.getSeconds()/3);
if (hh >= 0) {
document.all["clock"].innerText = ((mm < 10) ? "0" : "") + mm + ((ss < 10) ? ":0" : ":") + ss;
}
}
sourcedate = new Date(<?= date("Y,m,d,H,i,s",$seconds_left);?> );
//-->
</script>

Cheers - Pit

sharkblue
04-07-2004, 02:08 PM
Thank You Pit!, the getSeconds() has only 20 seconds in 1 minute now.

I tried to do the same for getMinutes() but it didn't work. I want only 30 minutes in 1 hour. I am sure that it's a tiny modification for getMinutes() too, but I just can't figure it out, I am a newbie at JavaScript.

I appreciate your help, but can you show me for the last time how to do the same for getMinutes()

THANK YOU! :)

Pittimann
04-09-2004, 10:40 AM
Hi sharkblue!

Sorry for the delay. I saw, that nobody dealt with your stuff, so here's some code:

<script language="JavaScript" type="text/javascript">
<!--
var ss, s, mm, m;
var sourcedate = new Date(<?= date("Y,m,d,H,i,s",$seconds_left);?> );
s= sourcedate.getSeconds();
m= sourcedate.getMinutes();
mm = parseInt(Number(m)/2);
ss = parseInt(Number(s)/3);
function showtime() {
ss--;
sourcedate.setTime(sourcedate.getTime()-2000);
if(ss==19){
mm--;
sourcedate.setTime(sourcedate.getTime()-30000);
}
s= sourcedate.getSeconds();
m= sourcedate.getMinutes();
var hh = (sourcedate.getDate()-1)*24 + sourcedate.getHours()-1;
if ( hh < 0 ) {
document.all["clock"].innerText = '';
this.location.href = this.location.href; // Reload the page
}
if (hh >= 0) {
document.all["clock"].innerText = ((mm < 10) ? "0" : "") + mm + ((ss < 10) ? ":0" : ":") + ss;
}
if(ss==0)ss=20;
if(mm==0&&ss==20){
mm=30;
}
sourcedate.setTime(sourcedate.getTime()-1000);
setTimeout("showtime();",1000);
}
//-->
</script>

Cheers - Pit

sharkblue
04-09-2004, 11:17 PM
thank you very much Pit! :D you were such a help for me! take care

Pittimann
04-09-2004, 11:19 PM
Hi!

You're welcome! Good luck for the future!

Cheers - Pit