Results 1 to 2 of 2

Thread: Javascript Count Up

  1. #1
    Join Date
    Feb 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript Count Up

    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 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:

    Code:
    <?php
    //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'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Telephone_1</th>
    <th>Telephone_2</th>
    <th>Telephone_3</th>
    <th>More Info</th>
    <th>Time Stamp</th>
    <th>How Old</th>
    </tr>";
    
    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>
    <script language=\"JavaScript\" src=\"http://scripts.hashemian.com/js/countdown.js\"></script>
    </td>";
      echo "</tr>";
      }
    echo "</table>";
    ?>
    Thanks in advance!

  2. #2
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default A quick n dirty

    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

    Code:
    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)
    	{
    	if(Month_Array[Pos]==CurrentDate[1])
    		{
    		month_number = Pos;
    		if(Pos>12)
    			{
    			break;
    			}
    		}
    	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";

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •