先看手册介绍:
strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳
格式:int strtotime ( string $time [, int $now ] )
本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。
本函数将使用 TZ 环境变量(如果有的话)来计算时间戳。自 PHP 5.1.0 起有更容易的方法来定义时区用于所有的日期/时间函数。此过程在 date_default_timezone_get() 函数页面中有说明。
Note : 如果给定的年份是两位数字的格式,则其值 0-69 表示 2000-2069,70-100 表示 1970-2000。
参数
time
被解析的字符串,格式根据 GNU » 日期输入格式 的语法。在 PHP 5.0 之前,time 中不允许有毫秒数,自 PHP 5.0 起可以有但是会被忽略掉。
now
用来计算返回值的时间戳。 该参数默认值是当前时间time(),也可以设置为其他时间的时间戳(我一直忽略的一个功能啊,惭愧)
返回值: 成功则返回间戳,否则返回 FALSE 。在 PHP 5.1.0 之前本函数在失败时返回 -1,后面版本返回false.
strtotime的第一个参数可以是我们常见的英文时间格式,比如“2008-8-20”或“10 September 2000 ”等等。也可以是以参数now为基准的时间描述,比如“+1 day”等等。
下面是后一种方式的可使用参数清单,其中“当前时间”是指strtotime第二个参数now的值,默认为当前时间
1.月,日英文名及其常用缩写清单:
january,february,march,april,may,june,july,august,september,sept,october,november,december,
sunday,monday,tuesday,tues,wednesday,wednes,thursday,thur,thurs,friday,saturday
2.时间参数和祥细描述:
am : the time is before noon 上午
pm : the time is noon or later 下午
year: one year; for example, “next year” 年,比如“next year”代表明年
month : one month; for example, “last month” 月,比如“last month”代表上一月
fortnight : two weeks; for example, “a fortnight ago” 两周,比如“a fortnight ago”代表两周前
week : one week 周
day: a day 天
hour: an hour 小时
minute : a minute 分钟
min : same as minute 同“minute”
second : a second 秒
sec : same as second 同“second”
3.相关和顺序说明:
+n/-n :以当前时间算,加个减指定的时间,比如”+1 hour”是指当前时间加一小时
ago :time relative to now; such as “24 hours ago” 以当前时间往前算,比如”24 hours ago”代表“24小时前”
tomorrow : 24 hours later than the current date and time 以当前时间(包括日期和时间)为标准,明天同一时间
yesterday : 24 hours earlier than the current date and time 以当前时间(包括日期和时间)为标准,昨天同一时间
today : the current date and time 当前时间(包括日期和时间)
now : the current date and time 当前时间(包括日期和时间)
last : modifier meaning “the preceding”; for example, “last tuesday” 代表“上一个”,比如“last tuesday”代表“上周二同一时间”
this : the given time during the current day or the next occurrence of the given time; for example, “this 7am” gives the timestamp for 07:00 on the current day, while “this week” gives the timestamp for one week from the current time 当天的指定时间或下面一个时间段的时间戳,比如“this 7am”给出当天7:00的时间戳,而“this week”给出的是从当前时间开始的一整周的时间戳,也就是当前时间(经本人测试:strtotime('this week')=strtotime('now'));
next : modifier meaning the current time value of the subject plus one; for example, “next hour” 当前时间加上指定的时间,比如“next hour”是指当前时间加上一小时,即加3600
//先到这,下面的还没时间翻译
first : ordinal modifier, esp. for months; for example, “May first” (actually, it's just the same as next)
third : see first (note that there is no “second” for ordinality, since that would conflict with the second time value)
fourth : see first
fifth : see first
sixth : see first
seventh : see first
eighth : see first
ninth : see first
tenth : see first
eleventh : see first
twelfth : see first
4.时区描述:
gmt : Greenwich Mean Time
ut : Coordinated Universal Time
utc : same as ut
wet : Western European Time
bst : British Summer Time
wat : West Africa Time
at : Azores Time
ast : Atlantic Standard Time
adt : Atlantic Daylight Time
est : Eastern Standard Time
edt : Eastern Daylight Time
cst : Central Standard Time
cdt : Central Daylight Time
mst : Mountain Standard Time
mdt : Mountain Daylight Time
pst : Pacific Standard Time
pdt : Pacific Daylight Time
yst : Yukon Standard Time
ydt : Yukon Daylight Time
hst : Hawaii Standard Time
hdt : Hawaii Daylight Time
cat : Central Alaska Time
akst : Alaska Standard Time
akdt : Alaska Daylight Time
ahst : Alaska-Hawaii Standard Time
nt : Nome Time
idlw : International Date Line West
cet : Central European Time
met : Middle European Time
mewt : Middle European Winter Time
mest : Middle European Summer Time
mesz : Middle European Summer Time
swt : Swedish Winter Time
sst : Swedish Summer Time
fwt : French Winter Time
fst : French Summer Time
eet : Eastern Europe Time, USSR Zone 1
bt : Baghdad Time, USSR Zone 2
zp4 : USSR Zone 3
zp5 : USSR Zone 4
zp6 : USSR Zone 5
wast : West Australian Standard Time
wadt : West Australian Daylight Time
cct : China Coast Time, USSR Zone 7
jst : Japan Standard Time, USSR Zone 8
east : Eastern Australian Standard Time
eadt : Eastern Australian Daylight Time
gst : Guam Standard Time, USSR Zone 9
nzt : New Zealand Time
nzst : New Zealand Standard Time
nzdt : New Zealand Daylight Time
idle : International Date Line East
在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函数用法。
相关推荐:
什么是seo运营,什么是seo营销 ,ai绘画沙地
AI免费工具:提升效率与创意的秘密武器
SEO管家:为您的网站保驾护航的智能SEO助手,网站推广作用有哪些类型
AI写作的崛起-“只能AI写作”背后的巨大潜力,舞狮摄影ai
好用的AI写作工具,提升写作效率与创意的最佳选择
ChatGPT常见问题汇总:解答你关于AI的一切疑惑,松鼠ai智能教适应教育
AI缩写文档:革新文档管理与自动化的未来,ai聊天中文
SEO百度优化:让你的品牌在搜索引擎中脱颖而出,日照网站推广策划
360AI写作怎样?助力创作的新风尚,ai到访
如何用AI写公众号文章?让创作更高效、更轻松
SEO运营是什么职业,seo运营工程师招聘 ,把ai图层改横幅
SEO培训:助力企业实现互联网营销的无限可能,平塘网站优化推广价格
SEO优化如何为网站做好关键词研究和优化,ai直通
WPS改写-轻松提升文档创作效率的秘密武器,推广网站的优势
SEO能给企业带来什么价值,seo的影响 ,ai蓝衣美女
SEO学费多少钱?揭秘SEO培训的投资价值与回报!,郴州网站推广多少钱一个
GoogleSEO打满分自然流量会高吗?揭开SEO优化的真相,猪插画ai
乘风SEO-引领企业互联网时代的腾飞之路,南昌b站关键词排名优化贵不贵
SEO调整,助力网站流量爆发式增长!,江都seo优化排名
打造高效创作体验,写文章AI软件重塑内容生产力
seo要学什么技术,seo要学什么技术好 ,ai.fale
SEO场景下的数字营销:如何通过精准优化提升网站流量,陕西融发建设集团网站
SEO提供:如何通过精准的SEO策略提升网站流量与品牌影响力,刷关键词排名立的火星
“扩写AI”-引领写作革命,开启智能创作新时代,dede seo 标题如何填写
ChatGPT5.0为什么一直没出来?背后的技术与战略,元宵节ai趣赏月
ChatGPT中文版下载免费版:智能对话新时代,尽在,ai光波
ChatGLM不能搜索网页内容,你真的了解它的局限性吗?,ai修复情侣
AI做文章:引领智能创作的未来
seo要什么条件,seo都需要做什么 ,交互ai求职
揭开“好的AI软件”背后的秘密:让生活和工作更智能的利器
ChatGPT不能访问,我的学术水平直线下降,泰州大数据ai艾灸价格
SEO教育:搜索引擎优化,开启成功职业之路,搜狗SEO排名接单
SEO搜外:全球搜索引擎优化的秘密武器,如何选择网站建设类型呢
SEO抓取:让您的网站从零到一的秘诀,佛山网站建设哪家效果好
seo菲律宾是做什么,菲律宾网址排名 ,openl ai
SEO快速排名实例:如何通过实战技巧提升网站排名,快速突破流量瓶颈,安徽ai跑步机企业
360关键:打造全方位安全保护,守护您的数字世界,ai乘bi
怎么让AI写文章,轻松实现内容创作的智能化
ChatGPT坏了用什么?替代方案,满足你的智能对话需求,星际一的ai
优化提长:让企业效能提升的秘密武器,长沙网站建设创意
AI智能时代的到来:如何利用人工智能推动生活与商业创新,ai相册下载
wordpress seo是什么,wordpress建站seo好做吗 ,学习图文ai
GPT-3模型下载:开启智能时代的无限可能,ai走入
SEO前的准备工作:如何让网站为搜索引擎优化做好充分准备,SEO_网站排名优化_网络推广
高效创作新时代AI文案速写工具,让创作更轻松
ChatGPT页面无法访问?解决方案,让你轻松摆脱困扰!,ai染发颜色
SEO合作:让你的网站在搜索引擎中脱颖而出,惠州抖音seo策划公司
SEO优化流程:助力网站快速提升排名的关键策略,1745ai
SEO而且:打造高效网站流量增长的秘密武器,滨江网站推广营销
SEO趋势:2025年搜索引擎优化的未来发展,河南省营销推广系统官网