Here is my function, and the date
$data->datecreated = getdate();
at the bellow code won’t get saved in the database. Why?
Notice that the record gets inserted, but the datecreated field looks like is ignored
public function generateCode($characters) {
//Generate the code
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
// Get the curent session id of the user
$session = JFactory::getSession();
$session_id = $session->getId();
//Create an stdClass
$data =new stdClass();
$data->sessionid = (integer)$session_id;
$data->captchacode = (string)$code;
$data->datecreated = getdate();
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->delete($db->nameQuote('#__captchasessions'));
$query->where($db->nameQuote('sessionid').'='.$db->quote($session_id));
$db->setQuery($query);
$db->query();
$db->insertObject( '#__captchasessions', $data, id );
return $code;
}
This is the structure of the table
delimiter $$
CREATE TABLE `ok6ut_captchasessions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sessionid` varchar(200) DEFAULT NULL,
`captchacode` varchar(10) DEFAULT NULL,
`datecreated` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8$$
getdate()returns an array with details of the current date in it, if there’s no timestamp passed in as a parameter.I expect that your database is expecting a formatted date to be passed in, and not an array – if you could add your database structure, that might help.
Edited to add:
For a datetime field, I think you need something like: