字符串日期时间插入到mysql数据库与代码点火器



我有以下MySQL表

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

我的PHP数组是

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

变量$when包含02/21/2013。当我尝试用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;
}

date外,所有内容都插入正确。你能帮我吗?

日期YYYY-MM-DD使用正确的MYSQL格式。例如,在代码中更改此项:
$data = array(
    'Title' => $title,
    'Description' => $description,
    'When' => date('Y-m-d', strtotime($when)),
    'Duration' => $duration,
    'Where' => $where
);

mySQL中的日期为YYYY-MM-DD

您正在插入日期MM/DD/YYYY

所以试试这个:

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

在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
);

相关内容

  • 没有找到相关文章

最新更新