admin管理员组文章数量:1321070
Can Anyone help me. I have problem insert into database when using convert date format. I am using codeigniter 4 and mysql database. below is my code:
foreach($worksheet_arr as $row){
// format date before convert = "1/15/2025"
$newDate = DateTime::createFromFormat("m/d/Y", $date);
$date = $newDate->format("Y-m-d");
$this->db->query("INSERT IGNORE INTO efakturheaderone (BRANCH, REFERENCE, DATE) VALUES (?, ?, ?)", [$row[0], $row[1], $date]);
}
but when I try dd($date);
there is no problem.
Then when I code below, there is also no problem:
foreach($worksheet_arr as $row){
$this->db->query("INSERT IGNORE INTO efakturheaderone (BRANCH, REFERENCE) VALUES (?, ?)", [$row[0], $row[1]);
}
Can Anyone help me. I have problem insert into database when using convert date format. I am using codeigniter 4 and mysql database. below is my code:
foreach($worksheet_arr as $row){
// format date before convert = "1/15/2025"
$newDate = DateTime::createFromFormat("m/d/Y", $date);
$date = $newDate->format("Y-m-d");
$this->db->query("INSERT IGNORE INTO efakturheaderone (BRANCH, REFERENCE, DATE) VALUES (?, ?, ?)", [$row[0], $row[1], $date]);
}
but when I try dd($date);
there is no problem.
Then when I code below, there is also no problem:
foreach($worksheet_arr as $row){
$this->db->query("INSERT IGNORE INTO efakturheaderone (BRANCH, REFERENCE) VALUES (?, ?)", [$row[0], $row[1]);
}
Share
Improve this question
edited Jan 18 at 8:12
Olivier
18.3k1 gold badge11 silver badges31 bronze badges
asked Jan 18 at 4:45
Mujab86Mujab86
94 bronze badges
4
|
1 Answer
Reset to default 0You can update your code like this.
Don't put insert in array, rather build your insert data and pass it to the query method.
Since your $date
isn't extract from the $worksheet_arr
, initialize the date function outside loop, if valid date, if not pass null. But ensure that your database DATE
column accepts null.
Updated with optional check, to only build insert values if the $date
is not empty.
<?php
$newDate = $date ? DateTime::createFromFormat("m/d/Y", $date)->format("Y-m-d") : null;
$inserts = [];
foreach ($worksheet_arr as $row) {
$inserts[] = [$row[0], $row[1], $newDate];
}
// Assuming the worksheet contains date in array index 2
// Use this method to insert only records that have valid date and remove the above foreach.
// Comment the continue line to insert all with date set to null if not valid.
//
// foreach ($worksheet_arr as $row) {
// if(!$row[2]){continue;}
// $newDate = isset($row[2]) ? DateTime::createFromFormat("m/d/Y", $date[2])->format("Y-m-d") : null;
// $inserts[] = [$row[0], $row[1], $newDate];
// }
if ($inserts !== []) {
$placeholders = implode(',', array_fill(0, count($inserts), '(?, ?, ?)'));
$values = [];
foreach ($inserts as $insert) {
$values = array_merge($values, $insert);
}
$this->db->query("
INSERT IGNORE INTO efakturheaderone (BRANCH, REFERENCE, DATE)
VALUES $placeholders",
$values
);
}
本文标签:
版权声明:本文标题:php - DateTime::createFromFormat(): Passing null to parameter #2 ($datetime) of type string is deprecated - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742081829a2419745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$date
variable getting value here$newDate = DateTime::createFromFormat("m/d/Y", $date);
?? – shirshak007 Commented Jan 18 at 5:08$mydate = "1/15/2025"; $newDate = DateTime::createFromFormat("m/d/Y", $mydate); $date = $newDate->format("Y-m-d");
– Mujab86 Commented Jan 18 at 5:17