Added date parser

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-05-31 02:14:50 +01:00
parent 52950f1322
commit afe4e5d62e

View file

@ -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;
}
}