4

I am having following MySQL table

EventId     ObjectKey   Title           Description     When        Duration        Where           Status
INT         CHAR(36)    VARCHAR(500)    VARCHAR(8000)   DATETIME    VARCHAR(500)    VARCHAR(500)    TINYINT

and my PHP array is

$data = array(
    'Title' => $title,
    'Description' => $description,
    'When' => $when,
    'Duration' => $duration,
    'Where' => $where
);

The variable $when contains 02/21/2013. When I try to insert to the table with CodeIgniter

public function insert_event($guid, $data)
{
    $CI = & get_instance();
    $CI->load->database('default');
    $CI->db->set($data);
    $CI->db->set('ObjectKey', $guid);
    $CI->db->set('Status', 1);
    $vari = $CI->db->insert('Events');
    return $vari;
}

Everything inserted correctly except date. Can you help me on this?

3 Answers 3

14

Use the correct MYSQL format for the date YYYY-MM-DD. For example change this in your code:

$data = array(
    'Title' => $title,
    'Description' => $description,
    'When' => date('Y-m-d', strtotime($when)),
    'Duration' => $duration,
    'Where' => $where
);
Sign up to request clarification or add additional context in comments.

Comments

4

Dates in mySQL are YYYY-MM-DD

You are inserting a date MM/DD/YYYY

So try this:

$data = array(
    'Title' => $title,
    'Description' => $description,
    'When' => date('Y-m-d', strtotime($when)),
    'Duration' => $duration,
    'Where' => $where
);

Comments

1

Simplest way to store string date in any format in mysql

$srcFormat = "m/d/Y"; //convert string to php date
$destFormat = "Y-m-d" //convert php date to mysql date string

$data = array(
  'Title' => $title,
  'Description' => $description,
  'When' => DateTime::createFromFormat($srcFormat, $when)->format($destFormat),
  'Duration' => $duration,
  'Where' => $where
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.