Jump to content

Recommended Posts

I am trying to insert the current time and date into fields of type time and date.  The line below works just fine.

 

mysql_query("INSERT INTO testtable (ID, CallTime, CallDate) VALUES ('1234', '12:12:12', '2001-01-01')");

 

When I insert the actual time and date as shown below the code below fails.

 

mysql_query("INSERT INTO testtable (ID, CallTime, CallDate) VALUES ('1234', date('G:i:s'), date('G:i:s'))");

 

If I echo date('G:i:s') and date('G:i:s') they look like they would work.

 

I'm guessing there is a formating problem here but need idea for how to make it work.

 

Thanks for a suggestion

Link to comment
https://forums.phpfreaks.com/topic/233725-beginners-question-about-time-and-data/
Share on other sites

date(), the way you are using it, is a php function. Putting it inside of a php string, that happens to be a SQL query statement, makes it the characters - d, a, t, e, (, ', G, :, i, :, s, ', ) and mysql will attempt to use the mysql date() function, which expects a mysql date or datetime value as a parameter.

 

Why not just use a single DATETIME field and use the mysql NOW() function in the query?

The reason I didn't choose a single  DATETIME field was not knowing how to write a query for all rows for say '2011-12-03' unless date was in a field by itself.

 

Sounds like using the DATETIME field is a better choice, but how to write the query?

 

Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.