🔧(helpers): Tambah fungsi parseTimestamp & perbaiki parsing tanggal

- Tambah fungsi parseTimestamp di app/Helpers/Lpj.php untuk parsing timestamp yang robust
- Gunakan native PHP DateTime & fallback ke Carbon untuk multiple format
- Validasi lengkap day, month, year, hour, minute, second dengan range aman
- Support format: Y-m-d H:i:s, Y-m-d, d/m/Y H:i:s, d/m/Y, d-m-Y, j-n-Y, dll
- Tambah logging detail & trim whitespace untuk reliability tinggi
- Return selalu format Y-m-d H:i:s agar konsisten di seluruh sistem
- signature-approval.blade.php gunakan parseTimestamp() untuk mig_mst_jaminan_tgl_laporan
- Tanggal migrasi kini diparsing & diformat dengan lebih akurat
- Hasil: parsing timestamp konsisten, error handling lebih kuat, dan log lebih informatif
This commit is contained in:
Daeng Deni Mardaeni
2025-10-28 14:02:50 +07:00
parent 73d0d238c0
commit 478c0c8079
2 changed files with 95 additions and 1 deletions

View File

@@ -595,3 +595,97 @@ function parsePembandingMigration($keterangan) {
}
return $path;
}
function parseTimestamp(?string $timestamp): ?string
{
if (!$timestamp) {
return null;
}
// Trim whitespace dan normalize
$timestamp = trim($timestamp);
// Log untuk debugging
Log::info('Mencoba parsing timestamp: "' . $timestamp . '"');
// Parsing dengan DateTime native PHP untuk lebih robust
try {
// Pattern untuk format d/m/Y H:i:s
if (preg_match('/^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(\d{1,2}):(\d{1,2}):(\d{1,2})$/', $timestamp, $matches)) {
$day = (int) $matches[1];
$month = (int) $matches[2];
$year = (int) $matches[3];
$hour = (int) $matches[4];
$minute = (int) $matches[5];
$second = (int) $matches[6];
// Validasi nilai
if ($day >= 1 && $day <= 31 && $month >= 1 && $month <= 12 && $year >= 1900 && $year <= 2100 &&
$hour >= 0 && $hour <= 23 && $minute >= 0 && $minute <= 59 && $second >= 0 && $second <= 59) {
// Buat DateTime object langsung
$dateTime = new \DateTime();
$dateTime->setDate($year, $month, $day);
$dateTime->setTime($hour, $minute, $second);
$result = $dateTime->format('Y-m-d H:i:s');
Log::info('Berhasil parsing dengan DateTime: ' . $timestamp . ' -> ' . $result);
return $result;
}
}
// Pattern untuk format d/m/Y tanpa waktu
if (preg_match('/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/', $timestamp, $matches)) {
$day = (int) $matches[1];
$month = (int) $matches[2];
$year = (int) $matches[3];
// Validasi nilai
if ($day >= 1 && $day <= 31 && $month >= 1 && $month <= 12 && $year >= 1900 && $year <= 2100) {
// Buat DateTime object langsung
$dateTime = new \DateTime();
$dateTime->setDate($year, $month, $day);
$dateTime->setTime(0, 0, 0);
$result = $dateTime->format('Y-m-d H:i:s');
Log::info('Berhasil parsing tanpa waktu dengan DateTime: ' . $timestamp . ' -> ' . $result);
return $result;
}
}
} catch (\Exception $e) {
Log::error('Gagal parsing dengan DateTime: ' . $timestamp . '. Error: ' . $e->getMessage());
}
// Fallback ke format Carbon standar untuk format lainnya
$formats = [
'Y-m-d H:i:s',
'Y-m-d',
'd-m-Y H:i:s',
'd-m-Y',
'j-n-Y H:i:s',
'j-n-Y',
];
foreach ($formats as $format) {
try {
$carbon = \Carbon\Carbon::createFromFormat($format, $timestamp);
if ($carbon && $carbon->format($format) === $timestamp) {
// Jika format tidak mengandung waktu, set ke awal hari
if (!str_contains($format, 'H:i:s')) {
$carbon = $carbon->startOfDay();
}
Log::info('Berhasil parsing dengan format ' . $format . ': ' . $timestamp . ' -> ' . $carbon->toDateTimeString());
return $carbon->toDateTimeString();
}
} catch (\Exception $e) {
// Lanjut ke format berikutnya
continue;
}
}
Log::error('Tidak dapat memparsing timestamp dengan format apapun: "' . $timestamp . '"');
return null;
}

View File

@@ -112,7 +112,7 @@
</span>
</br>
<span>
{{ isset($permohonan_migrasi->mig_mst_jaminan_tgl_laporan) ? formatTanggalIndonesia($permohonan_migrasi->mig_mst_jaminan_tgl_laporan) : '' }}
{{ isset($permohonan_migrasi->mig_mst_jaminan_tgl_laporan) ? formatTanggalIndonesia(parseTimestamp($permohonan_migrasi->mig_mst_jaminan_tgl_laporan)) : '' }}
</span>
</td>
@if($soUser->name==$eoUser->name)