Help inserting date and displaying date from mysql table

Discussion in 'PHP' started by learnwebsitedesigncom, Jan 4, 2011.

  1. #1
    Hi, I need help correctly inserting and displaying the current date from a mysql table. Here is the code that I'm using:

    
    <?php
    
     include('connect.php');
    
     $name = 'jose';
     $date = date('F d, Y');
    
     $insert = "
      INSERT INTO test VALUES ('', '$name', '$date')
     ";
     mysql_query($insert);
    
     $read = "
      SELECT * FROM test
     ";
     $result = mysql_query($read);
     while($row = mysql_fetch_assoc($result)){
      echo $row['name'] . ' ' . $row['date'] . '<br />';
     }
    ?>
    
    HTML:
    The table named 'test' has a field named 'name' and a field named 'date'. I am not able to correctly display the date. The date displays as '0000-00-00'. The field type for the date field is 'date'.

    Thanks
     
    learnwebsitedesigncom, Jan 4, 2011 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Change the 'date' field to int.
    Use the following code to insert :
    
    $name = 'jose';
    
    $insert = "INSERT INTO test VALUES ('', '$name', UNIX_TIMESTAMP())";
    mysql_query($insert);
    
    PHP:
    And the following code to display the table entries :
    
    $read = "SELECT * FROM test";
    $result = mysql_query($read);
    while($row = mysql_fetch_assoc($result)){
      echo $row['name'] . ' ' . date('F d, Y',$row['date']) . '<br />';
    }
    
    PHP:
     
    tvoodoo, Jan 5, 2011 IP
  3. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #3
    tvoodoo provides the best solution to you. You can also convert any date into Timestamp see time() function details on php.net.
     
    salmanshafiq, Jan 5, 2011 IP
  4. learnwebsitedesigncom

    learnwebsitedesigncom Active Member

    Messages:
    264
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Thanks for the help. I found out how to get the date using the mysql CURDATE() function. I also tried the methods which you guys showed and suggested and they also worked for me. Again thanks.
     
    learnwebsitedesigncom, Jan 5, 2011 IP