What is the proper php/mySQL syntax to insert a date/timestamp into a text column of mysql table using insert command

Posted on 2008-11-14
Last Modified: 2013-12-13
I've been trying to insert a record into a table using some values from a form, which was working until
i tried adding a timestamp field, it only seems to work if i put a static string in it. I though the code below should work, but it doesn't. Here is an example string that is contained in the variable today
"November 14, 2008, 8:46 am"
I figured maybe my problem is on the comma's or the colon, but don't know what to do about it.

$today = date("F j, Y, g:i a");

$query = "insert into myTable values (NULL,$today)";

 $result = $db->query($query);

I also tried using  SELECT NOW(); which works on the command line to mySQL, but again don't know how to syntax it correctly to be used as an input to the insert statement

The column i'm trying to insert it into is defined as char(30)
Question by:mitchguy
    LVL 2

    Expert Comment

    Try this
    November 14, 2008, 8:46 am
     $query = "insert into myTable values (NULL, DATE_FORMAT(NOW(), '%M %e, %Y, %h:%i %p'))";
    More info on
    Hope it helps!
    LVL 11

    Expert Comment

    To insert the current datetime


    $sql = "INSERT INTO table (datetime_timestamp_field) VALUES (NOW())";

    for your code the best way is use YEAR-MONTH-DAY HOUR:MIN:SEC

    $today = date("Y-m-d H:i:s");

    $query = "insert into myTable values (NULL,$today)";

     $result = $db->query($query);
    LVL 39

    Accepted Solution

    The problem with your query is that you have omitted the required apostrophes. This is wrong:

    $query = "insert into myTable values (NULL,$today)";

    It should be:

    $query = "insert into myTable values (NULL,'$today')";

    Author Comment

    The apostrophes worked!  
    A rookie mistake, I had tried that before, but forgot to try it again after changing my field from char(20) to char(30) after i noticed my string was longer than 20.

    Thanks for all of your inputs.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now