Skip to content

Commit

Permalink
fix parse date
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarzin committed Feb 28, 2025
1 parent bcefac2 commit 7b65213
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
22 changes: 20 additions & 2 deletions app/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public function getStartValidityAttribute($value)

public function setStartValidityAttribute($value)
{
$this->attributes['start_validity'] = $value ? Carbon::createFromFormat(config('panel.date_format'), $value)->format('Y-m-d') : null;
$this->attributes['start_validity'] =
$this->parseDate(
$value,
config('panel.date_format'));
}

public function getEndValidityAttribute($value)
Expand All @@ -55,7 +58,10 @@ public function getEndValidityAttribute($value)

public function setEndValidityAttribute($value)
{
$this->attributes['end_validity'] = $value ? Carbon::createFromFormat(config('panel.date_format'), $value)->format('Y-m-d') : null;
$this->attributes['end_validity'] =
$this->parseDate(
$value,
config('panel.date_format'));
}

public function getLastNotificationAttribute($value)
Expand All @@ -77,4 +83,16 @@ protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}

private function parseDate($value, $format = null)
{
$format = $format ? $format : config('panel.date_format');

try {
return $value ? Carbon::createFromFormat($format, $value)->format('Y-m-d') : null;
} catch (\Exception $e) {
Log::error('Invalid date format: ' . $value . ' with format ' . $format);
return null;
}
}
}
10 changes: 8 additions & 2 deletions app/MApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ public function getUpdateDateAttribute($value)

public function setUpdateDateAttribute($value)
{
$this->attributes['update_date'] = $this->parseDate($value);
$this->attributes['update_date'] =
$this->parseDate(
$value,
config('panel.date_format') . ' ' . config('panel.time_format'));
}

public function getInstallDateAttribute($value)
Expand All @@ -88,7 +91,10 @@ public function getInstallDateAttribute($value)

public function setInstallDateAttribute($value)
{
$this->attributes['install_date'] = $this->parseDate($value);
$this->attributes['install_date'] =
$this->parseDate(
$value,
config('panel.date_format') . ' ' . config('panel.time_format'));
}

public function getNextUpdateAttribute($value)
Expand Down

0 comments on commit 7b65213

Please sign in to comment.