PHP时间戳函数 strtotime()使用技巧
在php中我想要获取时间戳有多种方法,最常用的就是使用time函数与strtotime()函数把日期转换成时间戳了,下面我来给大家分享一下时间戳函数 strtotime用法.
获取指定的年月日转化为时间戳:
PHP时间戳函数获取指定日期的unix时间戳 strtotime('2012-12-7'),示例如下:
<?php echo strtotime('2012-12-7'); //结果:1354838400 ?>
说明: 返回2012年12月7日0点0分0秒时间戳,将当前年月日转化为时间戳,PHP时间戳函数获取当前日期的unix时间戳,示例如下:
<?php echo $time=intval(time()); ?>
说明: 返回当前时间 年月日时分秒的时间戳.
将时间戳转化为年月日,代码如下:
<?php
echo date("Y-m-d H:i:s",intval(time()));
?>
例,代码如下:
<?php /* from:http://www.phpfensi.com @date:2013-02-22 */ echo strtotime ( "now" ), "n" ; echo strtotime ( "10 September 2000" ), "n" ; echo strtotime ( "+1 day" ), "n" ; echo strtotime ( "+1 week" ), "n" ; echo strtotime ( "+1 week 2 days 4 hours 2 seconds" ), "n" ; echo strtotime ( "next Thursday" ), "n" ; echo strtotime ( "last Monday" ), "n" ; ?>PHP时间戳函数获取英文文本日期时间,便于比较,使用date将当时间戳与指定时间戳转换成系统时间.
(1)打印明天此时的时间戳strtotime(]+1 day])
(2)打印昨天此时的时间戳strtotime(]-1 day])
(3)打印下个星期此时的时间戳strtotime(]+1 week])
(4)打印上个星期此时的时间戳strtotime(]-1 week])
(5)打印指定下星期几的时间戳strtotime(]next Thursday])
(6)打印指定上星期几的时间戳strtotime(]last Thursday])
以上PHP时间戳函数示例strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间.
查看更多关于PHP时间戳函数 strtotime()使用技巧 - php日期的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29189