Welcome Guest, Not a member yet? Register   Sign In
Trouble inserting date in to datetime field
#1

[eluser]kyleect[/eluser]
I'm having trouble inserting the date from a form field in to the datetime field of my table. The code below takes any date string from the form and inserts it as '0000-00-00 00:00:00'. The `date` field is listed as a datetime field in MySQL. Can someone see something I'm overlooking for why this doesn't work? Thanks!

Model methods from MY_Model:
Code:
protected function convert_date_to_sqldate($date)
{
    return date('Y-m-d hh:mm:ss', strtotime($date));
}

public function add($insert_information = array(), $explict_types = array())
{
    if(count($insert_information) == 0)
    {
        return FALSE;
    }

    foreach($insert_information as $key => &$value)
    {
        if(isset($explict_types[$key]))
        {
            switch($explict_types[$key])
            {
                case 'date':
                $insert_information[$key] = $this->convert_date_to_sqldate($value);
                break;
            }
        }    
    }

    $this->db->insert($this->primary_table['name'], $insert_information);

    if($this->db->affected_rows() > 0)
    {            
        return $this->read($this->db->insert_id());
    }
    else
    {
            return FALSE;
    }
}

Code on page:
Code:
$insert_data = array(
    'date' => $this->input->post('date'),
    'amount' => $this->input->post('amount'),
    'account_number' => $this->input->post('account_number'),
    'type' => $this->input->post('transaction_type')
);
                        
$explicit_types = array('date' => 'date');
                        
if($query = $this->Transaction->add($insert_data, $explicit_types))
{
    redirect('transactions/browse');
}
else
{
    echo 'Insert failed';
}
#2

[eluser]Georgi Budinov[/eluser]
Perhaps your problem is here Smile

Code:
protected function convert_date_to_sqldate($date)
{
    return date('Y-m-d hh:mm:ss', strtotime($date));
}

to

Code:
protected function convert_date_to_sqldate($date)
{
    return date('Y-m-d H:m:i', strtotime($date));
}

Check this http://php.net/manual/en/function.date.php
#3

[eluser]kyleect[/eluser]
I changed i to s because the manual said i was minutes. It worked! Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB