View Full Version : Javascript Count Up

02-05-2007, 03:15 PM
Hi there, I have a php page that returns records for a particular mysql query. One of the fields is a timestamp of when the user was added to the database. I'm looking to be put in the right direction for a sample script I can use that for each record will count up how old the record is. I have used this script (http://scripts.hashemian.com/js/countdown.js) however it only seems to work for the last record.

Any help on what I can do to get the count up function working for all records would be greatly appreciated.

The code I have used is as follows:

//DB Connect
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("userdb") or die(mysql_error());

// Get all the data from the "users" table
$sql="select * from users";
$result = mysql_query($sql) or die(mysql_error());

echo "<table border='1'>
<th>More Info</th>
<th>Time Stamp</th>
<th>How Old</th>

while($row = mysql_fetch_array($result))
echo "<tr>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['Last_Name'] . "</td>";
echo "<td>" . $row['Telephone_1'] . "</td>";
echo "<td>" . $row['Telephone_2'] . "</td>";
echo "<td>" . $row['Telephone_3'] . "</td>";
echo "<td>" . $row['More Info'] . "</td>";
echo "<td>" . $row['Time_Stamp'] . "</td>";
echo "<td>" . $row['Time_Stamp'] . "
<script language=\"JavaScript\">
TargetDate = \"12/31/2001 5:00 AM\";
BackColor = \"palegreen\";
ForeColor = \"navy\";
CountActive = true;
CountStepper = 1;
LeadingZero = true;
DisplayFormat = \"%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.\";
FinishMessage = \"It is finally here!\";
<script language=\"JavaScript\" src=\"http://scripts.hashemian.com/js/countdown.js\"></script>
echo "</tr>";
echo "</table>";

Thanks in advance!

02-07-2007, 02:30 AM
The problem is a lot harder than I thought.

Here is a temporary solution. Whether the solution gets completed by me is a matter of my free time. Feel free to modify the code as I really have only done the minimal.

You may want to:
Add month lengths to correctly calc days,
Account for Leap years,
Change the output to include plurals (Yrs, not Yr) <-- I'm too lazy

Apart from that, I think the code should work.

Here is a possibility

TargetDate = TargetDate.split(" ");
TargetDate = TargetDate[0].split("/") //Target Date in array format where: TargetDate[0] = month, TargetDate[1] = day(number), TargetDate[2] = year

CurrentDate = new Date().toString().split(" "); //Date in array format where: CurrentDate[0] = Day(name), CurrentDate[1] = Month(name)..... day(number), year, time, etc etc

//Convert month name to month number
Month_Array = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Oct","Nov","Dec");
var Pos = 0;
var month_number = -1;
while(month_number == -1)
month_number = Pos;

//Calculate age
yearsOld = CurrentDate[3]-TargetDate[2];
monthsOld = (month_number>parseInt(TargetDate[0]))? month_number-parseInt(TargetDate[0]) : (12-parseInt(TargetDate[0]))+month_number;
daysOld = (parseInt(CurrentDate[2])>parseInt(TargetDate[1]))? parseInt(CurrentDate[2])-parseInt(TargetDate[1]) : (31-parseInt(TargetDate[1]))+parseInt(CurrentDate[2]);

//Take your pick from below or make your own
age_in_days = (yearsOld*365) + (monthsOld*30) + daysOld;
age_in_days_and_months = (yearsOld*12) + (monthsOld) + "Months " + daysOld +"Days";
age_full_with_abbr_units = yearsOld+"Yr " + monthsOld+"Mn " + daysOld+"Dy";