本篇文章给大家分享的是有关怎么获取php时间戳,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

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

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

代码实现:

<?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() ] )

参数:

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时间戳,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。