Skip to content

Commit

Permalink
Merge pull request #475 from statonlab/fix-site-import-date-issue
Browse files Browse the repository at this point in the history
fixes the date issue encountered when importing sites
  • Loading branch information
noah-77 committed Jul 17, 2023
2 parents 9faac11 + 2cceb93 commit 3a955b4
Show file tree
Hide file tree
Showing 2 changed files with 612 additions and 532 deletions.
23 changes: 14 additions & 9 deletions app/Imports/SiteImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
use App\Species;
use App\Measurement;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;

class SiteImport implements OnEachRow, WithHeadingRow, WithValidation
class SiteImport implements OnEachRow, WithHeadingRow, WithValidation, SkipsEmptyRows
{
/** @var Site $site */
protected $site;
Expand Down Expand Up @@ -99,14 +100,18 @@ public function onRow(Row $row)
$plant = $plant->first();
}

$date = null;

if (! is_int($row['date_mm_dd_yyyy'])) {
$date = new Carbon(str_replace('-', '/', $row['date_mm_dd_yyyy']));
} else {
/** @see http://www.cpearson.com/excel/datetime.htm#SerialDates */
$date = (new Carbon('Dec 31 1899'))->addDays($row['date_mm_dd_yyyy'] - 1);
}
$date = intval($row['date_mm_dd_yyyy']);
$date = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($date);
$date = Carbon::instance($date);

// $date = null;
// previous date logic, preserved for posterity (and in case it solved a problem i didn't notice)
// if (! is_int($row['date_mm_dd_yyyy'])) {
// $date = new Carbon(str_replace('-', '/', $row['date_mm_dd_yyyy']));
// } else {
// /** @see http://www.cpearson.com/excel/datetime.htm#SerialDates */
// $date = (new Carbon('Dec 31 1899'))->addDays($row['date_mm_dd_yyyy'] - 1);
// }

$exists = Measurement::withQuarantined()
->where('plant_id', $plant->id)
Expand Down
Loading

0 comments on commit 3a955b4

Please sign in to comment.