...

View Full Version : Js + Php



gavinjpearce
11-09-2004, 10:10 PM
hey all!

Just a few problems I've got stuck with, causing me a headache now lol, I'm sure it's something really simple.

I was trying to create a dynamic countdown script that updates every second but uses the server time rather than the client time, so i threw some JS and PHP together to create the below:



<script language="JavaScript">
year = '2004'
month = '12'
day = '1'
hour = '17'
minute = '00'
format = 1
{
html_code = '<span id="countdown"></span>';

document.write(html_code);

countdown(year, month, day, hour, minute, format);
}

function countdown(year, month, day, hour, minute, format)
{
Today = new Date();
Todays_Year = <?php echo date("Y"); ?> - 2000;
Todays_Month = <?php echo date("n"); ?> + 1;

//Convert both today's date and the target date into miliseconds.

<?php
echo "Todays_Date = (new Date(";
echo date("Y").",";
echo date("n").",";
echo date("j").",";
echo date("G").",";
echo date("i").",";
echo date("s");
echo ")).getTime();";
?>

Target_Date = (new Date(year, month, day, hour, minute, 00)).getTime();

//Find their difference, and convert that into seconds.
Time_Left = Math.round((Target_Date - Todays_Date) / 1000);

if(Time_Left < 0)
Time_Left = 0;

switch(format)
{
case 0:
//The simplest way to display the time left.
document.all.countdown.innerHTML = Time_Left + ' seconds';
break;
case 1:
//More datailed.
days = Math.floor(Time_Left / (60 * 60 * 24));
Time_Left %= (60 * 60 * 24);
hours = Math.floor(Time_Left / (60 * 60));
Time_Left %= (60 * 60);
minutes = Math.floor(Time_Left / 60);
Time_Left %= 60;
seconds = Time_Left;

dps = 's'; hps = 's'; mps = 's'; sps = 's';
//ps is short for plural suffix.
if(days == 1) dps ='';
if(hours == 1) hps ='';
if(minutes == 1) mps ='';
if(seconds == 1) sps ='';

document.all.countdown.innerHTML = days + ' day' + dps + ' ';
document.all.countdown.innerHTML += hours + ' hour' + hps + ' ';
document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and ';
document.all.countdown.innerHTML += seconds + ' second' + sps;
break;
default:
document.all.countdown.innerHTML = Time_Left + ' seconds';
}

//Recursive call, keeps the clock ticking.
setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
}
</script>

it used to update when it used to call the time from the client clock, but when I added the PHP to make it server-side, it now doesn't refresh adn update every second like it used to but it does display the correct countdown.

Why is this, and how can i fix it?

Brandoe85
11-10-2004, 01:06 AM
I didn't really look at your post in debt, but noticed you are using setTimeout(), which executes a function once after a specified number of miliseconds. If you want it to execute every second...you will want to use setInterval().

gavinjpearce
11-10-2004, 05:46 PM
Ok, i figured my first mistake, I included the PH in the loop and because PHP doesnt reload everytime it's refreshed, it wouldn't work... right so no I have it converted into miliseconds using PHP, and then let the JS do the rest. I no the PHP works at least :P just I can't figure out why the JS doesn't display the output right.

Example at http://gavinpearce.co.uk/dev/phpjs2.php

Cheers gain!



<html>
<head>
</head>
<body>
<script language="JavaScript">

html_code = '<span id="countdown"></span>';
document.write(html_code);

Todays_Date = <?php echo time()."\n";?>
//hou,min,sec,mon,day,yea
Target_Date = <?php echo mktime(17, 00, 0, 12, 1, 2004). "\n"; ?>
//Find their difference, and convert that into seconds.
Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
countdown(Time_Left);

function countdown(Time_Left)
{

if(Time_Left < 0)
Time_Left = 0;

days = Math.floor(Time_Left / (60 * 60 * 24));
Time_Left %= (60 * 60 * 24);
hours = Math.floor(Time_Left / (60 * 60));
Time_Left %= (60 * 60);
minutes = Math.floor(Time_Left / 60);
Time_Left %= 60;
seconds = Time_Left;

dps = 's'; hps = 's'; mps = 's'; sps = 's';
//ps is short for plural suffix.
if(days == 1) dps ='';
if(hours == 1) hps ='';
if(minutes == 1) mps ='';
if(seconds == 1) sps ='';

document.all.countdown.innerHTML = days + ' day' + dps + ' ';
document.all.countdown.innerHTML += hours + ' hour' + hps + ' ';
document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and ';
document.all.countdown.innerHTML += seconds + ' second' + sps;

Time_Left = Time_Left-1


//Recursive call, keeps the clock ticking.
setTimeout('countdown(' + Time_Left + ');', 1000);
}
</script>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum