-1

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. How do I write my code so that the date_time field gets inserted with the current datetime. The following is my code: (I know I can't call NOW() like that, but I wanted to show what I want to achieve)

$qcData = Array(
    "date_time" => NOW(),
    "note" => $note                                         
);

$qcNotes = new App_Model_DbTable_QCNotes();
$qcNotes->insert($qcData);
Adeel
  • 2,901
  • 7
  • 24
  • 34
Micheal
  • 2,272
  • 10
  • 49
  • 93

2 Answers2

5

here it is from the docs

26.3.1. Current Date Without any arguments, constructing an instance returns an object in the default locale with the current, local date using PHP's time() function to obtain the UNIX timestamp for the object. Make sure your PHP environment has the correct default timezone.

Example 26.2. Creating the Current Date

$date = new Zend_Date();

// Output of the current timestamp Feb 13, 2012 10:28:59 PM 
print $date;
//implement toString() output 2012-02-13 22:30:25
print $date->toString('YYYY-MM-dd HH:mm:ss');
RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • RockyFord I assigned $date_time=new Zend_Date(); but when I insert it displays 0000-00-00 00:00:00 in the database. I shall do a bit more debugging and come back.. – Micheal Feb 14 '12 at 03:24
  • @user856753 make sure your timezone is set correctly in the php.ini, you do know that most Db's will do this for you. In mysql for instance making a field a type = timestamp and attributes on update CURRENT_TIMESTAMP will automatically timestamp any updates. – RockyFord Feb 14 '12 at 03:31
  • could it be because the datetime format in the database table expects a different format than $date returns! – Micheal Feb 14 '12 at 04:10
4

You can use this:

Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss')
Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71