php strtotime 方法



strtotime() 是 PHP 中的一个函数,用于将任何英文文本日期时间描述解析为 Unix 时间戳。Unix 时间戳是自 1970 年 1 月 1 日(UTC)以来的秒数。

strtotime() 函数的语法如下:

strtotime(time, now)

参数说明:

  • time(必需):规定要解析的时间字符串。
  • now(可选):规定用来计算返回值的时间戳。如果省略该参数,则使用当前时间。

strtotime() 函数可以解析多种日期时间格式,例如:

  • "now" 或 "today":表示当前时间。
  • "tomorrow":表示明天的时间。
  • "yesterday":表示昨天的时间。
  • "next week" 或 "+1 week":表示下一周的时间。
  • "last week" 或 "-1 week":表示上周的时间。
  • "next month" 或 "+1 month":表示下一个月的时间。
  • "next year" 或 "+1 year":表示下一年的时间。

下面是一些示例:

echo strtotime("now"); // 输出当前时间的 Unix 时间戳  
  
echo strtotime("tomorrow"); // 输出明天的 Unix 时间戳  
  
echo strtotime("yesterday"); // 输出昨天的 Unix 时间戳  
  
echo strtotime("+1 week"); // 输出下周的 Unix 时间戳

请注意,strtotime() 函数还可以解析具体的日期时间字符串,例如 "2023-07-20 12:34:56"。它会将该日期时间解析为对应的 Unix 时间戳。