PHP中strtotime函数使用方法详解

时间:2025-10-20 01:45:37 php语言 我要投稿

PHP中strtotime函数使用方法详解

  在PHP中有个叫做strtotime的函数。strtotime 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳。strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间转化成unix时间戳]那么,PHP中strtotime函数使用方法有哪些?

  一,获取指定日期的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函数用法。</p

【PHP中strtotime函数使用方法详解】相关文章:

php强大的时间转换函数strtotime09-17

php中iconv函数使用方法08-10

PHP中session使用方法详解03-03

分析php中iconv函数使用方法11-23

php中date()日期时间函数使用方法03-12

php摘要生成函数详解02-17

PHP autoload函数的使用方法01-02

PHP时间和日期函数详解03-02

php的date()日期时间函数详解10-16

  • 相关推荐