mymotherland
mymotherland

Reputation: 8226

How to set current date and time in table field using zend

I am having table called users with following fields ,

is_login(tinyint(1)) and last_login(datetime).

Below is the piece of code to update when user is online using Zend,

public function updateLastLoginDetails($id){
            $currentDateTime = date('Y-m-d H:i:s');
            $data = array('is_online' => 1,'last_login'=>$currentDateTime);
            $this->_db->update( 'users', $data,'id = '.$id);
}

Here i am using $currentDateTime = date('Y-m-d H:i:s'); to store current data and time. But it seems not ok with time. Kindly suggest me the best way to store current data and time using Zend . Thanks in Advance, Dinesh Kumar Manoharan

Upvotes: 0

Views: 1813

Answers (3)

Lee
Lee

Reputation: 351

I'm not exactly sure what's causing your problem, but I find using NOW() to be easier. Also, you should ensure the variable $id gets quoted in the update's where condition.

public function updateLastLoginDetails($id){
    $data = array('is_online' => 1, 'last_login' => new Zend_Db_Expr('NOW()'));
    $this->_db->update('users', $data, array('id = ?' => $id));
}

Upvotes: 1

Gabriel Spiteri
Gabriel Spiteri

Reputation: 4978

Hi am I understanding correctly that the year month and day are being inserted correctly but not the hour minutes and seconds? If that is the case can you please provide use with a describe of the users table. Just execute "DESCRIBE users" in phpMyAdmin.

If that is the case it could be that the field is of type DATE and not DATETIME.

Upvotes: 0

James C
James C

Reputation: 14149

It looks like you forgot to use $data anywhere in your code!

public function updateLastLoginDetails($id){
    $currentDateTime = date('Y-m-d H:i:s');
    $data = array('is_online' => 1, 'last_login'=>$currentDateTime);
    $this->_db->update('users', $data, 'id = '. (int)$id);
}

You might want to consider using a TIMESTAMP field in place of a DATETIME for last_login and have MySQL update it automagically.

Upvotes: 0

Related Questions