From afe4e5d62e5edb6f9c7a7acf73b8b6ad8db15575 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 31 May 2024 02:14:50 +0100 Subject: [PATCH] Added date parser Signed-off-by: snipe --- app/Importer/Importer.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php index c7b0e96820..0d3f3ed623 100644 --- a/app/Importer/Importer.php +++ b/app/Importer/Importer.php @@ -6,6 +6,7 @@ use App\Models\CustomField; use App\Models\Department; use App\Models\Setting; use App\Models\User; +use Carbon\CarbonImmutable; use ForceUTF8\Encoding; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Auth; @@ -551,4 +552,35 @@ abstract class Importer return null; } + + /** + * Parse a date or return null + * + * @author A. Gianotto + * @since 7.0.0 + * @param $field + * @param $format + * @return string|null + + */ + public function parseOrNullDate($field, $format = 'date') { + + $format = 'Y-m-d'; + + if ($format == 'datetime') { + $date_format = 'Y-m-d H:i:s'; + } + + if (array_key_exists($field, $this->item) && $this->item[$field] != '') { + + try { + $value = CarbonImmutable::parse($this->item[$field])->format($date_format); + return $value; + } catch (\Exception $e) { + $this->log('Unable to parse date: ' . $this->item['next_audit_date']); + return null; + } + } + return null; + } }