HuntsvilleMan Posted April 14, 2011 Share Posted April 14, 2011 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 More sharing options...
PFMaBiSmAd Posted April 14, 2011 Share Posted April 14, 2011 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? Link to comment https://forums.phpfreaks.com/topic/233725-beginners-question-about-time-and-data/#findComment-1201596 Share on other sites More sharing options...
HuntsvilleMan Posted April 14, 2011 Author Share Posted April 14, 2011 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 Link to comment https://forums.phpfreaks.com/topic/233725-beginners-question-about-time-and-data/#findComment-1201606 Share on other sites More sharing options...
Muddy_Funster Posted April 14, 2011 Share Posted April 14, 2011 WHERE DATE(dateTimeFieldName) = '2011-12-03' should do the trick Link to comment https://forums.phpfreaks.com/topic/233725-beginners-question-about-time-and-data/#findComment-1201616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.