Skip to content

A free open source IT asset/license management system

Added date parser

snipeafe4e5d62e5e
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;
+ }
}