小兔网

php获取时间戳的方法:可以利用time()函数或strtotime()函数来获取。time()函数返回一个包含当前时间的Unix时间戳的整数。strtotime()函数将任何字符串的日期时间描述解析为Unix时间戳。

php怎么获取时间戳

time() 函数返回自 Unix 纪元(January 1 1970 00:00:00 GMT)起的当前时间的秒数。返回一个包含当前时间的 Unix 时间戳的整数。

(推荐教程:php图文教程

代码实现:

<?php$t=time();echo($t . "<br>");echo(date("Y-m-d",$t));?>

strtotime() 函数将任何字符串的日期时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。

语法:

int strtotime ( string $time [, int $now = time() ] )

(视频教程推荐:php视频教程

参数:

  • time 必需。规定日期/时间字符串。

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

代码实现:

<?php// 设置时区date_default_timezone_set("PRC");$time = strtotime("2018-01-18 08:08:08");  // 将指定日期转成时间戳 // 打印当前时间  PHP_EOL 换行符,兼容不同系统echo  $time, PHP_EOL; // 更多实例echo strtotime("now"), PHP_EOL;echo strtotime("now"), PHP_EOL;echo strtotime("10 September 2000"), PHP_EOL;echo strtotime("+1 day"), PHP_EOL;echo strtotime("+1 week"), PHP_EOL;echo strtotime("+1 week 2 days 4 hours 2 seconds"), PHP_EOL;echo strtotime("next Thursday"), PHP_EOL;echo strtotime("last Monday"), PHP_EOL;?>

以上就是php怎么获取时间戳的知识。速戳>>知识兔学习精品课!