Files
HRM-System/scratch/test_timezone_parsing.php

42 lines
1.5 KiB
PHP

<?php
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
use Carbon\Carbon;
$testStrings = [
'2026-05-05 10:11:00', // raw local format without offset
'2026-05-05T10:11:00+08:00', // ISO format with +08:00 offset
'Tue May 05 2026 10:11:00 GMT+0800 (China Standard Time)', // JS format with offset
'1770058260', // Unix timestamp for May 5, 2026 10:11:00 UTC+8 (which is 02:11:00 UTC)
];
foreach ($testStrings as $str) {
echo "--- Parsing: '$str' ---\n";
// Simulating parseDateSafe
$cleanDateStr = trim(explode('(', $str)[0]);
$parsed = null;
if (is_numeric($cleanDateStr)) {
$parsed = Carbon::createFromTimestamp($cleanDateStr);
} else {
$parsed = Carbon::parse($cleanDateStr);
}
echo "Parsed timezone: " . $parsed->tzName . "\n";
echo "Parsed time string: " . $parsed->toDateTimeString() . "\n";
// When saved to database, standard Laravel database driver uses $parsed->toDateTimeString()
// OR it might format it in UTC. Let's see:
$formattedForDb = $parsed->toDateTimeString();
echo "Formatted for DB: " . $formattedForDb . "\n";
// When retrieved from DB:
$retrieved = Carbon::parse($formattedForDb); // Laravel parses from DB
echo "Retrieved timezone: " . $retrieved->tzName . "\n";
echo "Retrieved time string: " . $retrieved->toDateTimeString() . "\n\n";
}