PHP中strtotime函数使用方法详解

yipeiwu_com5年前PHP代码库
在PHP中有个叫做strtotime的函数。strtotime 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳。strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间转化成unix时间戳]

一,获取指定日期的unix时间戳

strtotime("2009-1-22") 示例如下:
1.echo strtotime("2009-1-22")
结果:1232553600
说明:返回2009年1月22日0点0分0秒时间戳

二,获取英文文本日期时间

示例如下:
便于比较,使用date将当时间戳与指定时间戳转换成系统时间
(1)打印明天此时的时间戳strtotime("+1 day")
当前时间:
1.echo date("Y-m-d H:i:s",time())
结果:2009-01-22 09:40:25
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("+1 day"))
结果:2009-01-23 09:40:25
(2)打印昨天此时的时间戳strtotime("-1 day")
当前时间:
1.echo date("Y-m-d H:i:s",time())
结果:2009-01-22 09:40:25
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("-1 day"))
结果:2009-01-21 09:40:25
(3)打印下个星期此时的时间戳strtotime("+1 week")
当前时间:
1.echo date("Y-m-d H:i:s",time())
结果:2009-01-22 09:40:25
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("+1 week"))
结果:2009-01-29 09:40:25
(4)打印上个星期此时的时间戳strtotime("-1 week")
当前时间:
1.echo date("Y-m-d H:i:s",time())
结果:2009-01-22 09:40:25
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("-1 week"))
结果:2009-01-15 09:40:25
(5)打印指定下星期几的时间戳strtotime("next Thursday")
当前时间:
1.echo date("Y-m-d H:i:s",time())
结果:2009-01-22 09:40:25
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("next Thursday"))
结果:2009-01-29 00:00:00
(6)打印指定上星期几的时间戳strtotime("last Thursday")
当前时间:
1.echo date("Y-m-d H:i:s",time())
结果:2009-01-22 09:40:25
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("last Thursday"))
结果:2009-01-15 00:00:00
以上示例可知,strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间。
希望通过本文的介绍后,你已经能掌握strtotime函数用法。

相关文章

PHPExcel冻结(锁定)表头的简单实现方法

PHPExcel冻结(锁定)表头的简单实现方法

本文实例讲述了PHPExcel冻结(锁定)表头的简单实现方法。分享给大家供大家参考,具体如下: PHPExcel是一款功能比较强大的操作微软excel的PHP插件,在导出数据时为了方便查...

二招解决php乱码问题

二招解决php乱码问题

php网页出现乱码一般是在建立数据库时用的编码和php网页的编码不同造成的, 用phpmyadmin建立的数据库如果你不指定编码他默认是latin1_swedish_ci 编码,既瑞典语...

php文件上传类完整实例

本文实例讲述了php文件上传类。分享给大家供大家参考,具体如下: /** $file=new class_file($file_array,"flash/"); $file->...

Php Cookie的一个使用注意点

复制代码 代码如下:<?php setcookie('test', 'this is a cookie test'); echo ($_COOKIE['test']); ?>...

PHP迭代器接口Iterator用法分析

本文实例讲述了PHP迭代器接口Iterator用法。分享给大家供大家参考,具体如下: PHP Iterator接口的作用是允许对象以自己的方式迭代内部的数据,从而使它可以被循环访问,It...