一、设置时区
date_default_timezone_set(‘Asia/Shanghai’);//设置时区
二、获取当前Unix时间戳
time()
三、获取指定时间的Unix时间戳
mktime()
date_default_timezone_set(‘Asia/Shanghai’);//设置时区
$nowTime=time();
t
i
m
e
1
=
m
k
t
i
m
e
(
0
,
0
,
0
,
10
,
1
,
2014
)
;
/
/
取
得
一
个
日
期
的
U
n
i
x
时
间
戳
e
c
h
o
′
距
离
2014
年
国
庆
还
有
′
.
(
(
time1=mktime(0,0,0,10,1,2014);//取得一个日期的 Unix 时间戳 echo '距离2014年国庆还有'.((
time1=mktime(0,0,0,10,1,2014);//取得一个日期的Unix时间戳echo′距离2014年国庆还有′.((time1-$nowTime)/60/60/24).‘天’;
四、从Unix时间戳取得时间日期信息
date()格式化一个本地时间/日期
第一个参数:必填,写上你所需要的时间日期的格式,把format 字符放在第一个参数里面会被转换成对应的信息,其他的字符还是原来的样子
date(‘Y-m-d G:i:s’)
自定义格式化Unix时间戳 为指定的时间格式 !
五、获取Unix时间戳和微秒数
microtime() 返回当前 Unix 时间戳和微秒数
var_dump(microtime()); 前面是微妙数,后面是时间戳
var_dump(microtime(true)); 这种方法用的比较方便 前面是时间戳,后面是微妙数
判断for循环执行多长时间
<?php
date_default_timezone_set('Asia/Shanghai');//设置时区
$startTime=microtime(true);
for ($i=0;$i<10000000;$i++){
}
$endTime=microtime(true);
echo '该for循环花费了'.round(($endTime-$startTime),4).'秒';