SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Finding how old a record is in a mysql database

    Hi everyone! This is my first post to these forums!
    It looks like you keep it busy!

    I am making a php script that will open a mysql database, and check how old each record is.
    Here is what I have done...

    PHP Code:
    <?php

    $dbcnx 
    = @mysql_connect("SERVERHERE""USERNAMEHERE""PASSWORDHERE");
    if (!
    $dbcnx) { echo( "Unable to connect to database at this time<br>" );
    exit(); }

    mysql_select_db("DATABASENAMEHERE"$dbcnx);
    $result = @mysql_query("SELECT Autoincrement, Date_Added FROM TABLEHERE");

    $currentdate=date('Y-m-d');

    while ( 
    $row mysql_fetch_array($result) ) {
    $old $currentdate $row["Date_Added"];
    echo(
    "Record $row["Autoincrement"] is $old days old.<br>");
    }

    ?>
    It does not work. Can someone please tell me what I did wrong.

    Thanks!

  2. #2
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this will return the error

    PHP Code:
    $result = @mysql_query("SELECT Autoincrement, Date_Added FROM TABLEHERE") or die(mysql_error()); 
    tell us what you get back
    http://redgoals.com ... my site ... nuff said

  3. #3
    SitePoint Member
    Join Date
    Mar 2002
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, turned out the field was not titled "Date_Added" it is DATEADDED.
    I made that change, but it does not count the days correctly...
    0 days old.
    0 days old.
    1 days old.
    0 days old.
    0 days old.
    0 days old.
    0 days old.
    0 days old.
    0 days old.
    0 days old.
    is what it displays.

    Here is the current code...

    PHP Code:
    <?php
    $dbcnx 
    = @mysql_connect("DATABASEHERE""USERNAMEHERE""PASSWORDHERE");
    if (!
    $dbcnx) { echo( "Unable to connect to database at this time<br>" );
    exit(); }
    mysql_select_db("DATABASENAMEHERE"$dbcnx);
    $result = @mysql_query("SELECT DATEADDED FROM TABLENAMEHERE") or die(mysql_error());
    $currentdate=date('Y/m/d');
    while ( 
    $row mysql_fetch_array($result) ) {
    $old $currentdate $row["DATEADDED"];
    echo(
    "$old days old.<br>");
    }
    ?>

  4. #4
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe you need to extract the DATEADDED column in the same format as currentdate.


    is DATEADDED in the format as timestamp?
    http://redgoals.com ... my site ... nuff said

  5. #5
    SitePoint Member
    Join Date
    Mar 2002
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what the script is looking for, for today's date: 2002/03/17
    All records in the database are that same format.
    Here is one: 2001/10/15
    It says it is 1 day old.

  6. #6
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if the DATEADDED was in timestamp format then you could subtract like so:

    PHP Code:
    $result = @mysql_query("select to_days(now()) - to_days(DATEADDED) as date FROM TABLENAMEHERE") or die(mysql_error());
    while ( 
    $row mysql_fetch_array($result) ) {
    echo 
    $row["date"]; 
    Last edited by redgoals; Mar 17, 2002 at 16:47.
    http://redgoals.com ... my site ... nuff said

  7. #7
    SitePoint Member
    Join Date
    Mar 2002
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So there is no way to find out how old they are in the format of year/month/day ?

  8. #8
    SitePoint Member
    Join Date
    Mar 2002
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Can someone give me a answer??


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
  •