Inserting Date problem

Discussion in 'PHP' started by ashrafzia, May 14, 2008.

  1. #1
    I want to insert current date and due date for the invoices in the database.
    I have tried alot but didn't get any fruitful result.
    I am providing the user with due_date filed and the current date should insert automatically with the record.
    The datatypes of the current_date and the due_date both are DATE.
    I want to clear one more thing, should i specify the date format to the user to use?
    
    $c_date=date("Y-m-d");
    $d_date='$_post[due_date]'; //just putting value from text box
     
    $sql= "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}',
        service_description='{$_POST['s_desc']}', comments='{$_POST['comments']}', init_price='{$_POST['i_price']}',
        price='{$_POST['price']}', adjustments='{$_POST['adj']}', total_price='{$_POST['t_price']}',
        current_date='$date' , due_date='{$_POST['d_date']}' "; 
    
    PHP:
    I don't know what to write for the due_date in the query.

    Please help.
     
    ashrafzia, May 14, 2008 IP
  2. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    My first suggestion would be to store the dates as Unix time - so INTs instead of DATEs - and parse them into a human readable a date when they are pulled back out (date('M-d-Y', $db_value)). This is best done by providing the user with a drop down menu for Month (in text), day and year, then combining them all. I can send you some code for this, if you'd like.

    That aside, you'd just put your variable ($d_date) into the query.
    INSERT INTO `table` (`column1`, `column2`, `due_date`) VALUES ('$value1', '$value2', '$d_date');
    Code (markup):
    Remember to sterilize information that is entered via a form using mysql_real_escape_string. You shouldn't be inserting $_POST variables directly.

    It's early, and if I'm not making sense let me know. =)
     
    Altari, May 14, 2008 IP
  3. DeFacto

    DeFacto Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You need to put hidden field in your form, and that is that.

    
    <?php
    $c_date=date("Y-m-d");
    ?>
    <form>
    
    ........
    
    
    <input type="hidden" name="some_name" value="<?php echo $c_date; ?>" >
    </form>
    
    Code (markup):
    Hope this is what you are looking for.
     
    DeFacto, May 14, 2008 IP
  4. ashrafzia

    ashrafzia Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What is the datatype for unix time stamp? Is it TIMESTAMP?
    If yes then I have to make both of my fields of current date and due date as TIMESTAMP.
    And kindly show me some code(query), how to insert date from the list box or a text box (in case we suggest user to write your date in the specific format).
     
    ashrafzia, May 14, 2008 IP
  5. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I sent you a very long PM with the code. ;)

    For the database, you'll want the "date" columns to be INT, not TIMESTAMP. It's easier for comparison purposes.
     
    Altari, May 14, 2008 IP