PHP date() & strtotime()

交互式参考:测试格式字符串、查找参数并尝试 strtotime() 表达式。

date(" ")
{{ dateResult }}
<?php echo date("{{ dateFormat }}"); ?>
{{ __t('th_character') }} {{ __t('th_description') }} {{ __t('th_example') }}
strtotime(" ")
{{ strtotimeResult.timestamp }}
{{ strtotimeResult.formatted }}
{{ strtotimeResult.relative }}
{{ strtotimePhpCode }}
{{ __t('alert_expression_not_recognized') }}
{{ cat.title }}
{{ __t('th_expression') }} {{ __t('th_description') }} {{ __t('label_result') }}
{{ ref.expr }} {{ ref.desc }} {{ getStrtotimeExample(ref.expr) }}

PHP date() — 格式参数

函数 date(string $format, ?int $timestamp = null) 将 Unix 时间戳或当前时间格式化为可读字符串。格式字符串由占位符组成,如 Y(四位数年份)、m(带前导零的月份)、d(日)或 H:i:s(24小时制的时:分:秒)。

PHP strtotime() — 相对时间表达式

strtotime(string $datetime, ?int $baseTimestamp = null) 将英文文本描述转换为 Unix 时间戳。典型输入:"now""+1 day""next monday""first day of next month""last day of december 2025"。该函数非常灵活,还能理解组合表达式,如 "+2 weeks 3 days"

常用格式

  • date("Y-m-d") — ISO 日期: 2025-03-15
  • date("d.m.Y") — 德语格式: 15.03.2025
  • date("Y-m-d H:i:s") — MySQL Datetime
  • date("c") — ISO 8601: 2025-03-15T14:30:00+01:00
  • date("r") — RFC 2822: Sat, 15 Mar 2025 14:30:00 +0100
  • date("U") — Unix 时间戳