<?php
$units = [
    'nanosecond'  => ['name' => '纳秒 (ns)', 'factor' => 1e-9],
    'microsecond' => ['name' => '微秒 (μs)', 'factor' => 1e-6],
    'millisecond' => ['name' => '毫秒 (ms)', 'factor' => 0.001],
    'second'      => ['name' => '秒 (s)', 'factor' => 1],
    'minute'      => ['name' => '分钟 (min)', 'factor' => 60],
    'hour'        => ['name' => '小时 (h)', 'factor' => 3600],
    'day'         => ['name' => '天 (d)', 'factor' => 86400],
    'week'        => ['name' => '周 (wk)', 'factor' => 604800],
    'month'       => ['name' => '月 (月)', 'factor' => 2629800], // 平均月 (365.25/12 * 86400)
    'year'        => ['name' => '年 (yr)', 'factor' => 31557600], // 平均年 (365.25 * 86400)
    'decade'      => ['name' => '十年 (decade)', 'factor' => 315576000],
    'century'     => ['name' => '世纪 (century)', 'factor' => 3155760000],
];

$results = array_fill_keys(array_keys($units), 0);
$inputValue = '';
$sourceUnit = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['unit'])) {
    $sourceUnit = $_POST['unit'];

    if (isset($_POST['values'][$sourceUnit]) && isset($units[$sourceUnit])) {
        
        $raw_input = $_POST['values'][$sourceUnit];
        $validated_input = filter_var($raw_input, FILTER_VALIDATE_FLOAT);

        if ($validated_input !== false) {
            $inputValue = $validated_input;

            $valueInSeconds = $inputValue * $units[$sourceUnit]['factor'];

            foreach ($units as $key => $unit) {
                if ($unit['factor'] > 0) {
                    $results[$key] = $valueInSeconds / $unit['factor'];
                } else {
                    $results[$key] = 0;
                }
            }
        } else {
            $inputValue = '';
        }
    }
}

function format_number($number) {
    if ($number == 0) {
        return '0';
    }
    if (abs($number) > 0 && (abs($number) < 1e-6 || abs($number) > 1e12)) {
        return sprintf('%.6e', $number);
    }
    $formatted = number_format($number, 8, '.', '');
    return rtrim(rtrim($formatted, '0'), '.');
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <title>在线时间单位换算器 | 秒,分钟,小时,天,周,年换算</title>
    <meta name="description" content="一个强大且易于使用的在线时间单位换算工具，支持纳秒、毫秒、秒、分钟、小时、天、周、月、年、世纪等单位之间的快速相互转换。">
    <meta name="keywords" content="时间换算, 单位换算, 时间转换器, 在线工具, 秒, 分钟, 小时, 天, 年">
    <meta name="author" content="Claude Sonnet 4">
    
    <link rel="stylesheet" href="css/style.css">
    
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
</head>
<body>

    <main class="converter-container">
        <header class="converter-header">
            <svg class="icon" width="48" height="48" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2ZM12 20C7.58172 20 4 16.4183 4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20Z" fill="#cccccc"/>
                <path d="M12 6V12L16 14" stroke="#007bff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <h1>时间单位换算</h1>
        </header>

        <form action="index.php" method="post" class="converter-form" novalidate>
            <?php foreach ($units as $key => $unit): ?>
            <div class="unit-row">
                <label for="input-<?php echo $key; ?>" class="unit-label"><?php echo htmlspecialchars($unit['name']); ?></label>
                <div class="input-group">
                    <input 
                        type="number" 
                        id="input-<?php echo $key; ?>"
                        name="values[<?php echo $key; ?>]"
                        step="any" 
                        class="unit-input"
                        placeholder="输入数值"
                        value="<?php echo ($sourceUnit === $key) ? htmlspecialchars((string)$inputValue) : ''; ?>"
                    >
                    <span class="unit-result"><?php echo format_number($results[$key]); ?></span>
                </div>
                <button type="submit" name="unit" value="<?php echo $key; ?>" class="convert-button">转换</button>
            </div>
            <?php endforeach; ?>
        </form>
        
        <footer class="converter-footer">
            <p><strong>提示:</strong> 在任意输入框中填入数值，然后点击右侧的“转换”按钮，即可看到所有单位的换算结果。</p>
            <p><small>* “月”和“年”的换算是基于平均值 (1年 = 365.25天)，结果可能存在微小误差。</small></p>
        </footer>
    </main>
    <script>
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?a84b405270f45bb9044b5f7792b32009";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
    </script>
</body>
</html>
