php怎么将时间格式转换时间戳

来自:互联网
时间:2021-03-22
阅读:

PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是:

  • strtotime():将任何英文文本的日期时间描述解析为时间戳。

  • mktime():从日期取得时间戳。

strtotime()

strtotime() 函数用于将英文文本字符串表示的日期转换为时间戳,为 date() 的反函数,成功返回时间戳,否则返回 FALSE 。语法:

int strtotime ( string time [, int now] )
  • time 必需。规定日期/时间字符串,是根据 GNU 日期输入格式表示的日期。

  • now 可选。规定用来计算返回值的时间戳。如果省略该参数,则使用当前时间。

返回值: 成功则返回时间戳,失败则返回 FALSE。

例子:

<?php
echo strtotime("2009-10-21 16:00:10"), "<br />";//输出 1256112010
echo strtotime("10 September 2008"), "<br />";//输出 1220976000
echo strtotime("+1 day"), "<br />";//输出明天此时的时间戳
?>

输出:

1256112010
1220976000
1616496304

mktime()

mktime() 函数用于从日期取得时间戳,成功返回时间戳,否则返回 FALSE 。语法:

mktime(hour,minute,second,month,day,year,is_dst);

php怎么将时间格式转换时间戳

例子:

<?php
echo mktime(21, 50, 55, 07, 14, 2021);//输出“1626270655”
?>

参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。

返回顶部
顶部