Help with PHP and Mysql dates?

Discussion in 'PHP' started by virtualmisc, Feb 11, 2010.

  1. #1
    Hi friends

    Can anybody advise on how i can best use php and mysql to give a database record an "age" in days?

    I am recording record creation via timestamp at the minute


    Thanks
     
    virtualmisc, Feb 11, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    just use the mysql date and time functions to work out the age between now and the time inserted, then output that
    google mysql date and time functions
     
    JAY6390, Feb 11, 2010 IP
  3. roy.laurie

    roy.laurie Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    http://codepad.org/bCYtCYmX
    
    <?php
    // Calculate the days since a MySQL timestamp.
    // Roy Laurie Software < http://www.roylaurie.com >
    
    $datestamp = '2000-02-04 01:34:00'; // get this from mysql
    $unixstamp = strtotime($datestamp);
    $secondsDiff = time() - $unixstamp;
    $days = $secondsDiff / 60 / 60 / 24;
    $days = floor($days);
    
    echo "It has been {$days} days since {$datestamp}.\n";
    
    PHP:
    :)
     
    roy.laurie, Feb 11, 2010 IP
  4. systematical

    systematical Peon

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here are the two best ways to do this.

    MySQL Perspective:
    
    INSERT INTO my_table SET date_time = NOW()
    
    Code (markup):
    That will give date_time something that looks like '2010-02-11 23:59:59'

    PHP Perspective:
    
    $sql = "INSERT INTO my_table SET date_time = '".date('Y-m-d H:i:s')."'";
    mysql_query($sql);
    
    Code (markup):
    I prefer using the mysql function NOW(). Using the mysql timestamp option is bad because everytime that record is updated it will update that time. If you want that the I suggest having a created_date_time column and a updated_date_time column.
     
    systematical, Feb 11, 2010 IP