简体   繁体   English

在 Laravel 7 中导入 excel 文件时显示错误“日期值无效”

[英]Showing error "Invalid date value" while importing excel file in Laravel 7

I want to import data from an Excel file.我想从 Excel 文件中导入数据。 While inserting data, I want to calculate some value depending on the requested Excel file.在插入数据时,我想根据请求的 Excel 文件计算一些值。 But the time difference is not returning the correct date format.但是时差没有返回正确的日期格式。

Controller:控制器:

public function import(Request $request){
  $request->validate([
    'import_file'=>'required'
   ]);
   Excel::import(new ImportAttendanceExcel,request()->file('import_file'));
   return back()->with('message','XL File Import Successfully!');

 }

Model:模型:

class ImportAttendanceExcel implements ToModel, WithStartRow
{
    public function model(array $row)
    {   $id = 1;
        $settings = Setting::find($id);
        $open =  Carbon::parse($settings->opening_time);
        $end =   Carbon::parse($settings->ending_time);
        $date = Carbon::parse(Date::excelToDateTimeObject ($row[2]));
        $user = $row[0];
        $exist_attd = Attendance::where('user_id', $user)->where('attendance_date', $date)->get()->toArray();
            if($row[0] != null){
                if(empty($exist_attd)){
                    $attendances = new Attendance;
                    $attendances->user_id = $row[0];
                    $attendances->created_by = Auth::user()->id;
                    $attendances->attendance_date =Carbon::parse(Date::excelToDateTimeObject ($row[2]));
                    $attendances->attendance_status  = $row[3];
                    $attendances->check_in = Carbon::parse(Date::excelToDateTimeObject ($row[4]));
                    $attendances->check_out = Carbon::parse(Date::excelToDateTimeObject ($row[5]));
                    $csv_check_in = Carbon::parse(Date::excelToDateTimeObject($row[4]));
                    $csv_check_out = Carbon::parse(Date::excelToDateTimeObject($row[5]));
                    if( $csv_check_in > $open){
                        $late_count = $csv_check_in->diff($open)->format('%H:%I:%S');
                        $attendances->late = $late_count;
                    }
                    if( $csv_check_out < $end){
                        $early_count = $csv_check_out->diff($end)->format('%H:%I:%S');
                        $attendances->early = $early_count;
                    }

                 $attendances->save();
                }

            }
    }


    public function startRow(): int
    {
        return 2;
    }

}

Return Value with error:有错误的返回值:

(SQL: insert into `attendances` (`user_id`, `created_by`, `attendance_date`, `attendance_status`, `check_in`, `check_out`, `early`, `updated_at`, `created_at`) values (2, 1, 44713, 1, 0.42361111111111, 0.77430555555556, 23:59:59, 2022-06-14 15:12:47, 2022-06-14 15:12:47))

Excel File: Excel文件:

id ID date日期 status地位 check-in报到 check-out查看
1 1 2022-06-10 2022-06-10 1 1 10:15:00 10:15:00 18:00:00 18:00:00

Here the date format and the calculation is not working properly.这里的日期格式和计算无法正常工作。 How can I solve this?我该如何解决这个问题?

You cant just directly throw date from excel to DB.您不能直接将日期从 excel 扔到 DB。 You need to format it first.您需要先对其进行格式化。 Since laravel-excel is built from phpSpreadsheet.由于 laravel-excel 是从 phpSpreadsheet 构建的。 You can import Date Class您可以导入日期类

use \PhpOffice\PhpSpreadsheet\Shared\Date;

// Rest of your code

$date = Date::excelToDateTimeObject($yourExcelDate);

// Rest or your code

From the PHPSpreadsheet docs :来自PHPSpreadsheet 文档

\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($excelDate) \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($excelDate)

Converts a date from an Excel date/timestamp to return a PHP DateTime object.从 Excel 日期/时间戳转换日期以返回 PHP DateTime对象。

My problem is solved by this :我的问题由此解决:

class ImportAttendanceExcel implements ToModel, WithStartRow
    {
        public function model(array $row)
        {   $id = 1;
            $settings = Setting::find($id);
            $open =  Carbon::parse($settings->opening_time);
            $end =   Carbon::parse($settings->ending_time);
            $date = Carbon::parse(Date::excelToDateTimeObject ($row[2]));
            $user = $row[0];
            $exist_attd = Attendance::where('user_id', $user)->where('attendance_date', $date)->get()->toArray();
                if($row[0] != null){
                    if(empty($exist_attd)){
                        $attendances = new Attendance;
                        $attendances->user_id = $row[0];
                        $attendances->created_by = Auth::user()->id;
                        $attendances->attendance_date =Carbon::parse(Date::excelToDateTimeObject ($row[2]));
                        $attendances->attendance_status  = $row[3];
                        $attendances->check_in = Carbon::parse(Date::excelToDateTimeObject ($row[4]));
                        $attendances->check_out = Carbon::parse(Date::excelToDateTimeObject ($row[5]));
                        $csv_check_in = Carbon::parse(Date::excelToDateTimeObject($row[4]));
                        $csv_check_out = Carbon::parse(Date::excelToDateTimeObject($row[5]));
                        if( $csv_check_in > $open){
                            $late_count = $csv_check_in->diff($open)->format('%H:%I:%S');
                            $attendances->late = $late_count;
                        }
                        if( $csv_check_out < $end){
                            $early_count = $csv_check_out->diff($end)->format('%H:%I:%S');
                            $attendances->early = $early_count;
                        }
    
                     $attendances->save();
                    }
    
                }
        }
    
    
        public function startRow(): int
        {
            return 2;
        }
    
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM