gmdate()
按 UTC 格式化时间,不受默认时区影响。
gmdate(string $format, ?int $timestamp = null): string
参数
| 参数 | 类型 | 说明 |
|---|---|---|
$format | string | 格式串 |
$timestamp | ?int | 时间戳 |
返回值
string — UTC 时间字符串。
示例
echo gmdate('Y-m-d H:i:s', 0);
date_default_timezone_set('Asia/Shanghai');
echo date('Y-m-d H:i:s', 0);输出
1970-01-01 00:00:00 1970-01-01 08:00:00
注意
- 存数据库、写日志、对外接口建议统一用 UTC,展示时再转本地。
- HTTP 头里的 Date 必须是 GMT,用 gmdate('D, d M Y H:i:s') . ' GMT'。