Jump to content

storing current date and time in datetime format


php_begins

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.