代码里有时候经常会进行一些与时间有关的操作,在这里整理一下关于时间相关的方法。
获取当前时间戳
time()
以秒为单位,是一个int类型的unix时间戳
int(1514532263)
microtime()
是一个字符串,前半部分为小数部分,后半部分是整数部分
string(21) "0.03409500 1514532362"
microtime(true)
以秒为单位,是一个float类型的时间戳
float(1514532362.0341)
字符串转换成时间戳
strtotime('now')
当前时间
int(1514532765)
strtotime('+1 day')
一天后
int(1514619165)
strtotime('1 December 2017')
,2017年12月1号
int(1512057600)
strtotime('-1 day')
一天前
int(1514446365)
格式化本地时间
date('Y-m-d H:i:s')
,将当前时间以指定格式显示,返回的是一个字符串
2017-12-29 15:45:39
date('Y-m-d H:i:s', strtotime('-1 day'))
,将前一天的时间戳以固定格式显示
2017-12-28 15:45:39