search
Homephp教程php手册php中strtotime()函数的用法

php中strtotime()函数的用法

Jun 13, 2016 am 11:19 AM
200phpstrtotimeunixonefunctiondesignationdatetimeusageofObtain

 

一,获取指定日期的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()格式化日期时间获取指定的时间戳,实现所需要的日期时间。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool