Rumah > Artikel > pembangunan bahagian belakang > 详细介绍PHP中strtotime函数使用方法_PHP教程
在PHP中有个叫做strtotime的函数。strtotime 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳。strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间转化成unix时间戳]
一,获取指定日期的unix时间戳
strtotime("2009-1-22") 示例如下:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">strtotime</span><span>(</span><span class="string">"2009-1-22"</span><span>) </span></span></li></ol>
结果:1232553600
说明:返回2009年1月22日0点0分0秒时间戳
二,获取英文文本日期时间
示例如下:
便于比较,使用date将当时间戳与指定时间戳转换成系统时间
(1)打印明天此时的时间戳strtotime("+1 day")
当前时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,time()) </span></span></li></ol>
结果:2009-01-22 09:40:25
指定时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,</span><span class="func">strtotime</span><span>(</span><span class="string">"+1 day"</span><span>)) </span></span></li></ol>
结果:2009-01-23 09:40:25
(2)打印昨天此时的时间戳strtotime("-1 day")
当前时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,time()) </span></span></li></ol>
结果:2009-01-22 09:40:25
指定时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,</span><span class="func">strtotime</span><span>(</span><span class="string">"-1 day"</span><span>)) </span></span></li></ol>
结果:2009-01-21 09:40:25
(3)打印下个星期此时的时间戳strtotime("+1 week")
当前时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,time()) </span></span></li></ol>
结果:2009-01-22 09:40:25
指定时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,</span><span class="func">strtotime</span><span>(</span><span class="string">"+1 week"</span><span>)) </span></span></li></ol>
结果:2009-01-29 09:40:25
(4)打印上个星期此时的时间戳strtotime("-1 week")
当前时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,time()) </span></span></li></ol>
结果:2009-01-22 09:40:25
指定时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,</span><span class="func">strtotime</span><span>(</span><span class="string">"-1 week"</span><span>)) </span></span></li></ol>
结果:2009-01-15 09:40:25
(5)打印指定下星期几的时间戳strtotime("next Thursday")
当前时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,time()) </span></span></li></ol>
结果:2009-01-22 09:40:25
指定时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,</span><span class="func">strtotime</span><span>(</span><span class="string">"next Thursday"</span><span>)) </span></span></li></ol>
结果:2009-01-29 00:00:00
(6)打印指定上星期几的时间戳strtotime("last Thursday")
当前时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,time()) </span></span></li></ol>
结果:2009-01-22 09:40:25
指定时间:
<ol class="dp-c"><li class="alt"><span><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d H:i:s"</span><span>,</span><span class="func">strtotime</span><span>(</span><span class="string">"last Thursday"</span><span>)) </span></span></li></ol>
结果:2009-01-15 00:00:00
以上示例可知,strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间。
希望通过本文的介绍后,你已经能掌握strtotime函数用法。