> For the complete documentation index, see [llms.txt](https://help.holytreasure.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.holytreasure.space/kai-fang-jie-kou-api/shi-shi-shi-jian.md).

# 实时时间

**接口来由：**

其实关于这个实时的时间API是否要开发我也犹豫很久，但是最后还是决定要去开发。理由是网站最终还是要适应全球化，要不然有时候翻个墙回头一看时间出乱导致程序崩溃。传统的HTML的开发时使用js他是基于用户本地的时间来决定的，因此开发一个基于东八区的时间API还是有必要的。

**1. 接口概述**

`time.php` 是一个用于获取服务器当前时间戳和详细时间信息的简单 RESTful API。该接口返回当前时间的时间戳以及详细的年、月、日、时、分、秒信息。

**2. 请求 URL**

```
http://api.holytreasure.top/time.php
http://api.holytreasure.space/time.php
```

* **替换 `yourdomain.com` 为你的实际域名或 IP 地址**。

**3. 请求方法**

* **方法**: GET

**4. 返回数据格式**

* **内容类型**: `application/json`
* **响应示例**:

```json
{
    "status": "success",
    "timestamp": 1694725740,
    "datetime": {
        "year": "2024",
        "month": "09",
        "day": "14",
        "hour": "19",
        "minute": "49",
        "second": "00"
    }
}
```

**5. 响应状态码**

* **200 OK**: 请求成功，返回当前时间的相关信息。
* **其他状态码**: 如果出现其他问题，将返回相应的 HTTP 状态码。

**6. 错误处理**

* **请求失败**: 如果出现任何错误，响应将包含一个错误对象，如下所示：

```json
{
    "status": "error",
    "message": "An error occurred while fetching the current time."
}
```

**7. 使用示例**

**7.1 JavaScript 示例**

```javascript
function loadTime() {
    fetch('http://api.holytreasure.top/time.php') // 替换为你的服务器地址
        .then(response => response.json())
        .then(data => {
            const timeInfoElement = document.getElementById('timeInfo');
            timeInfoElement.innerHTML = `
                时间戳: ${data.timestamp}<br>
                年: ${data.datetime.year}<br>
                月: ${data.datetime.month}<br>
                日: ${data.datetime.day}<br>
                时: ${data.datetime.hour}<br>
                分: ${data.datetime.minute}<br>
                秒: ${data.datetime.second}<br>
            `;
        })
        .catch(error => {
            console.error('Error:', error);
            alert('加载时间失败，请稍后再试。');
        });
}

// 页面加载时立即加载一次时间
window.onload = loadTime;
```

**7.2 HTML + JavaScript 示例**

```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时时间</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }
        .container {
            text-align: center;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            margin-bottom: 10px;
        }
        p {
            font-size: 1.2em;
            color: #666;
        }
        button {
            background-color: #007BFF;
            color: white;
            padding: 10px 20px;
            font-size: 1em;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>实时时间</h1>
        <p id="timeInfo"></p>
        <button onclick="loadTime()">刷新时间</button>
    </div>

    <script>
        function loadTime() {
            fetch('http://api.holytreasure.top/time.php') // 替换为你的服务器地址
                .then(response => response.json())
                .then(data => {
                    const timeInfoElement = document.getElementById('timeInfo');
                    timeInfoElement.innerHTML = `
                        时间戳: ${data.timestamp}<br>
                        年: ${data.datetime.year}<br>
                        月: ${data.datetime.month}<br>
                        日: ${data.datetime.day}<br>
                        时: ${data.datetime.hour}<br>
                        分: ${data.datetime.minute}<br>
                        秒: ${data.datetime.second}<br>
                    `;
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('加载时间失败，请稍后再试。');
                });
        }

        // 页面加载时立即加载一次时间
        window.onload = loadTime;
    </script>
</body>
</html>
```
