php_begins Posted February 14, 2012 Share Posted February 14, 2012 I have a field called date_time in the database which is of type datetime. I have a form which on submit needs to enter the current date and time in the date_time field. I know I can get the current time using time() $time=time(); Can do I do some kind of manipulation using strtotime() and store in the database in datetime format? Quote Link to comment https://forums.phpfreaks.com/topic/257093-storing-current-date-and-time-in-datetime-format/ Share on other sites More sharing options...
Pikachu2000 Posted February 14, 2012 Share Posted February 14, 2012 If you're using MySQL, there's a function for that: NOW() Quote Link to comment https://forums.phpfreaks.com/topic/257093-storing-current-date-and-time-in-datetime-format/#findComment-1317947 Share on other sites More sharing options...
php_begins Posted February 14, 2012 Author Share Posted February 14, 2012 oh ok. So just do: "INSERT INTO (datetime) VALUES (NOW())"; ? Quote Link to comment https://forums.phpfreaks.com/topic/257093-storing-current-date-and-time-in-datetime-format/#findComment-1317948 Share on other sites More sharing options...
jcbones Posted February 14, 2012 Share Posted February 14, 2012 If datetime is your column name, you may have to surround it in backticks. $sql = "INSERT INTO (`datetime`) VALUES (NOW())"; Quote Link to comment https://forums.phpfreaks.com/topic/257093-storing-current-date-and-time-in-datetime-format/#findComment-1317951 Share on other sites More sharing options...
php_begins Posted February 14, 2012 Author Share Posted February 14, 2012 I actually I ll have to reframe my question. I dont think I can use NOW() with the way I am writing my code now. $qcData = Array( "date_time" => NOW(), "note" => $note ); $qcNotes = new App_Model_DbTable_QCNotes(); $qcNotes->insert($qcData); Quote Link to comment https://forums.phpfreaks.com/topic/257093-storing-current-date-and-time-in-datetime-format/#findComment-1317975 Share on other sites More sharing options...
Psycho Posted February 14, 2012 Share Posted February 14, 2012 I actually I ll have to reframe my question. I dont think I can use NOW() with the way I am writing my code now. $qcData = Array( "date_time" => NOW(), "note" => $note ); $qcNotes = new App_Model_DbTable_QCNotes(); $qcNotes->insert($qcData); You're looking at it the wrong way. Don't try and SET the value before you run the query - just use NOW() IN the query and the database will insert the correct time. So, just pass the string of 'NOW()' - but if your insert method does any parsing of the values this might not work $qcData = Array( "date_time" => 'NOW()', "note" => $note ); $qcNotes = new App_Model_DbTable_QCNotes(); $qcNotes->insert($qcData); However, there is a MUCH, MUCH easier solution. Just set up your datetime field in the database to automatically use the current timestamp as the default value. Then you do not need to pass/set the value at all - it will be done automatically Quote Link to comment https://forums.phpfreaks.com/topic/257093-storing-current-date-and-time-in-datetime-format/#findComment-1317976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.