Jump to content

Beginner's question about time and data


HuntsvilleMan

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.