API文档
提供完整的REST API,方便开发者在自己的应用中集成时间戳转换功能
基本信息
API基础URL
https://timestamptodate.com/api/v1
数据格式
JSON
字符编码
UTF-8
HTTP方法
GET, POST
时间戳转日期
日期转时间戳
当前时间
批量转换
历史记录
格式列表
时间戳转日期
将时间戳转换为指定格式的日期字符串
接口信息
| URL | GET /api/v1/timestamp |
| 描述 | 将时间戳转换为指定格式的日期字符串 |
| 参数 |
|
请求示例
GET https://timestamptodate.com/api/v1/timestamp?timestamp=1640995200&format=Y-m-d
响应示例
{
"success": true,
"timestamp": 1640995200,
"timestamp_ms": 1640995200000,
"results": [
{
"format": "Y-m-d",
"description": "指定格式",
"date": "2022-01-01"
}
]
}
时间戳转多种格式
将时间戳转换为多种常用日期格式
接口信息
| URL | GET /api/v1/timestamp/formats |
| 描述 | 将时间戳转换为多种常用日期格式 |
| 参数 |
|
请求示例
GET https://timestamptodate.com/api/v1/timestamp/formats?timestamp=1640995200
响应示例
{
"success": true,
"timestamp": 1640995200,
"timestamp_ms": 1640995200000,
"results": [
{
"format": "Y-m-d H:i:s",
"description": "标准格式",
"date": "2022-01-01 00:00:00"
},
{
"format": "Y年m月d日 H:i:s",
"description": "中文格式",
"date": "2022年01月01日 00:00:00"
},
{
"format": "D, d M Y H:i:s",
"description": "RFC 2822",
"date": "Sat, 01 Jan 2022 00:00:00"
}
]
}
日期转时间戳
将日期字符串转换为时间戳
接口信息
| URL | GET /api/v1/date |
| 描述 | 将日期字符串转换为时间戳 |
| 参数 |
|
支持的日期格式
- Y-m-d H:i:s(标准格式)
- Y-m-d(短日期)
- m/d/Y H:i:s(美国格式)
- d/m/Y H:i:s(欧洲格式)
- Y-m-dTH:i:sP(ISO 8601)
请求示例
GET https://timestamptodate.com/api/v1/date?date=2022-01-01
响应示例
{
"success": true,
"date": "2022-01-01",
"timestamp": 1640995200,
"timestamp_ms": 1640995200000
}
获取当前时间戳
获取当前时间的时间戳和多种格式表示
接口信息
| URL | GET /api/v1/current |
| 描述 | 获取当前时间的时间戳和多种格式表示 |
| 参数 |
|
请求示例
GET https://timestamptodate.com/api/v1/current
响应示例
{
"success": true,
"timestamp": 1698886400,
"timestamp_ms": 1698886400000,
"date": "2023-11-01 12:00:00",
"date_ms": "2023-11-01 12:00:00.123",
"iso": "2023-11-01T12:00:00+08:00",
"rfc": "Wed, 01 Nov 2023 12:00:00 +0800",
"timezone": "Asia/Shanghai",
"utc_offset": "+08:00"
}
批量转换
批量转换多个时间戳
接口信息
| URL | POST /api/v1/batch |
| 描述 | 批量转换多个时间戳 |
| 请求体 |
|
请求示例
POST https://timestamptodate.com/api/v1/batch
Content-Type: application/json
{
"timestamps": [1640995200, 1641081600, 1641168000],
"format": "Y-m-d"
}
响应示例
{
"success": true,
"total": 3,
"results": [
{
"input": "1640995200",
"success": true,
"data": {
"format": "Y-m-d",
"description": "批量转换格式",
"date": "2022-01-01"
},
"error": null
},
{
"input": "1641081600",
"success": true,
"data": {
"format": "Y-m-d",
"description": "批量转换格式",
"date": "2022-01-02"
},
"error": null
},
{
"input": "1641168000",
"success": true,
"data": {
"format": "Y-m-d",
"description": "批量转换格式",
"date": "2022-01-03"
},
"error": null
}
]
}
获取转换历史
获取转换历史记录
接口信息
| URL | GET /api/v1/history |
| 描述 | 获取转换历史记录 |
| 参数 |
|
请求示例
GET https://timestamptodate.com/api/v1/history?limit=10
响应示例
{
"success": true,
"history": [
{
"id": 1,
"timestamp_input": "1640995200",
"timestamp_value": 1640995200,
"date_result": "2022-01-01 00:00:00",
"format": "Y-m-d H:i:s",
"ip_address": "127.0.0.1",
"user_agent": "Mozilla/5.0...",
"created_at": "2023-11-01 12:00:00"
}
]
}
获取常用格式
获取系统预设的常用日期格式
接口信息
| URL | GET /api/v1/formats |
| 描述 | 获取系统预设的常用日期格式 |
| 参数 | 无 |
请求示例
GET https://timestamptodate.com/api/v1/formats
响应示例
{
"success": true,
"formats": [
{
"id": 1,
"name": "ISO 8601",
"format": "Y-m-d\\TH:i:sP",
"description": "国际标准格式",
"is_active": 1,
"sort_order": 1
},
{
"id": 2,
"name": "中文日期",
"format": "Y年m月d日 H:i:s",
"description": "中文日期时间",
"is_active": 1,
"sort_order": 2
}
]
}
获取时区列表
获取系统支持的时区列表
接口信息
| URL | GET /api/v1/timezones |
| 描述 | 获取系统支持的时区列表 |
| 参数 | 无 |
请求示例
GET https://timestamptodate.com/api/v1/timezones
响应示例
{
"success": true,
"timezones": [
{
"value": "Asia/Shanghai",
"label": "Asia/Shanghai",
"offset": "+08:00"
},
{
"value": "UTC",
"label": "UTC",
"offset": "+00:00"
}
]
}
错误处理
API 使用标准的HTTP状态码和统一的错误响应格式
HTTP状态码
200- 请求成功400- 请求参数错误404- 接口不存在500- 服务器内部错误
错误响应格式
{
"success": false,
"error": "错误信息"
}
常见错误
| 错误信息 | 说明 |
| 请提供时间戳参数 | 缺少时间戳参数 |
| 无效的时间戳 | 无效的时间戳 |
| 请提供日期参数 | 缺少日期参数 |
| 无效的日期格式 | 无效的日期格式 |
| 请提供时间戳数组 | 批量转换缺少时间戳数组 |
使用示例
JavaScript
// 时间戳转日期
fetch('https://timestamptodate.com/api/v1/timestamp?timestamp=1640995200')
.then(response => response.json())
.then(data => {
if (data.success) {
console.log(data.results[0].date);
}
});
// 批量转换
fetch('https://timestamptodate.com/api/v1/batch', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
timestamps: [1640995200, 1641081600]
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
data.results.forEach(result => {
console.log(result.input, result.success ? result.data.date : result.error);
});
}
});
PHP
// 时间戳转日期
$response = file_get_contents('https://timestamptodate.com/api/v1/timestamp?timestamp=1640995200');
$data = json_decode($response, true);
if ($data['success']) {
echo $data['results'][0]['date'];
}
// 批量转换
$data = [
'timestamps' => [1640995200, 1641081600],
'format' => 'Y-m-d'
];
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode($data)
]
];
$context = stream_context_create($options);
$response = file_get_contents('https://timestamptodate.com/api/v1/batch', false, $context);
$result = json_decode($response, true);
Python
import requests
import json
# 时间戳转日期
response = requests.get('https://timestamptodate.com/api/v1/timestamp?timestamp=1640995200')
data = response.json()
if data['success']:
print(data['results'][0]['date'])
# 批量转换
data = {
'timestamps': [1640995200, 1641081600],
'format': 'Y-m-d'
}
response = requests.post('https://timestamptodate.com/api/v1/batch', json=data)
result = response.json()
if result['success']:
for item in result['results']:
if item['success']:
print(item['input'], item['data']['date'])