最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - DateTime::createFromFormat(): Passing null to parameter #2 ($datetime) of type string is deprecated - Stack Overflow

programmeradmin6浏览0评论

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 How is your $date variable getting value here $newDate = DateTime::createFromFormat("m/d/Y", $date); ?? – shirshak007 Commented Jan 18 at 5:08
  • Sorry, I'm typo, actually like this the code $mydate = "1/15/2025"; $newDate = DateTime::createFromFormat("m/d/Y", $mydate); $date = $newDate->format("Y-m-d"); – Mujab86 Commented Jan 18 at 5:17
  • 1 Please, edit your question – shirshak007 Commented Jan 18 at 5:19
  • What error do you get – Rohit Gupta Commented Jan 19 at 5:08
Add a comment  | 

1 Answer 1

Reset to default 0

You 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
    );
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论