php获取时间戳的方法:可以利用time()函数或strtotime()函数来获取。time()函数返回一个包含当前时间的Unix时间戳的整数。strtotime()函数将任何字符串的日期时间描述解析为Unix时间戳。
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 起的秒数)。
语法:
立即学习“PHP免费学习笔记(深入)”;
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;e cho 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怎么获取时间戳的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号