Fix Carbon parsing for JS date strings with timezone names
This commit is contained in:
38
test-sync.php
Normal file
38
test-sync.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
require __DIR__.'/vendor/autoload.php';
|
||||
$app = require_once __DIR__.'/bootstrap/app.php';
|
||||
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
|
||||
$kernel->bootstrap();
|
||||
|
||||
function parseDateSafe($dateStr) {
|
||||
if (!$dateStr) return null;
|
||||
if (is_numeric($dateStr)) {
|
||||
if (strlen((string)$dateStr) > 10) return \Carbon\Carbon::createFromTimestampMs($dateStr);
|
||||
return \Carbon\Carbon::createFromTimestamp($dateStr);
|
||||
}
|
||||
try {
|
||||
return \Carbon\Carbon::parse($dateStr);
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$attendances = [
|
||||
['deviceUserId' => '2006', 'recordTime' => '2026-05-19 14:00:00'],
|
||||
['id' => '1', 'timestamp' => '2026-05-19T08:00:00.000Z']
|
||||
];
|
||||
|
||||
$formattedAttendances = [];
|
||||
foreach ($attendances as $record) {
|
||||
$empCode = $record['deviceUserId'] ?? $record['id'] ?? null;
|
||||
$rawPunchTime = $record['recordTime'] ?? $record['timestamp'] ?? null;
|
||||
$punchTime = parseDateSafe($rawPunchTime);
|
||||
|
||||
if ($empCode && $punchTime) {
|
||||
$formattedAttendances[] = [
|
||||
'emp_code' => (string) $empCode,
|
||||
'punch_time' => $punchTime->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
var_dump($formattedAttendances);
|
||||
Reference in New Issue
Block a user