Results 1 to 4 of 4

Thread: Date fields added in mysql

  1. #1
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Date fields added in mysql

    Hi,

    I'm trying to add a field "date" in mysql db.
    To this field, will be added a date which becomes from a form by selecting a javascript calendar and inputing it to a textbox.

    I've got 2 questions:

    1- What format would i get in mysql for date format "D,F j,Y", i.e:

    Wednesday, May 28, 2008

    2- I have this code to get the value from the form and then other code to insert it into the DB.

    PHP Code:
    $post_date date("D,F j,Y"$info['post_date']); 
    But, for some reason, I can't add this value to the DB in mysql.
    I've got this fieldset in mysql:

    project_done - text(30) NOT NULL

    and have this error after I select a day in calendar:

    Warning: date() expects parameter 2 to be long, string given in /.../post.php on line 114.
    In line 114 here's the code:

    PHP Code:
    $post_date date("D,F j,Y"$info['post_date']); 
    Can you help me setting up this date fields???

    Thanks

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    If you make the project_done a DATE_TIME, and then use the NOW() function(mysql).
    Jeremy | jfein.net

  3. #3
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, thanks for your idea, but this post_date it's not the now() date, it supose to be a future date, which users may select from javascript calendar.

    I want to sabe that date in my db so I can output it on other form which can be seen by other users.

    Thanks anyway.

  4. #4
    Join Date
    May 2007
    Location
    By the beach
    Posts
    23
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    The PHP date() module expects the second paramiter to be a Unix time stamp. The PHP error message posted was caused by passing a string as the second parameter.

    E.g.
    //date(string format, [int timestamp]); // www.php.net/date
    echo date("D, F j Y", time()); //Thu, May 8 2008


    Better Method
    1. Convert MySQL text column to date using SQL:

    ALTER TABLE `tablename` CHANGE `project_done` `project_done` DATE NULL;
    -- reference: http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
    -- reference: http://dev.mysql.com/doc/refman/5.0/en/datetime.html

    2. Adjust JavaScript calender settings to use YYYY-MM-DD date formate (this is possible on most dynamic drive calendar scripts)
    3. Adjust your insert/update PHP SQL:

    //PHP Insert Example
    $sql = sprintf("INSERT INTO `tablename` (`project_done`) VALUES ('%s')", $_POST['post_date']);

    //PHP Update
    $sql = sprintf("UPDATE `tablename` set `project_done` = '%s' WHERE foo = bar", $_POST['post_date']);


    Now you can both work with the date in JavaScript and MySQL. MySQL has many very usefull date/time functions that make working with dates easy and almost fun.

    MySQL Date/Time Functions: http://dev.mysql.com/doc/refman/5.0/...functions.html

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
  •