时区的获取和设置
header('content-type:text/html;charset=utf-8;');
echo "当前时区为:".date_default_timezone_get()."
";
var_dump(date_default_timezone_set("PRC"));
echo "
";
echo "当前时区为:".date_default_timezone_get();
echo "
";
ini_set('date.timezone','Asia/Shanghai');
echo "当前时区为:".ini_get('date.timezone');
date函数的参数解析
header("content_type:text/html;charset=utf-8;");
echo date('Y/m/d H:i:s')."
";
echo date('y-n-j g:i:s A')."
";
echo date('w')."
";
echo date("L")."
";
echo date('Y')."年";
echo date("L")?"是闰年":"不是闰年";
echo "
";
echo "本周是全年的第".date('W')."周
";
echo "今天是全年的第".date('z')."天
";
时间戳的使用
header("content-type:text/html;charset=utf-8;");
echo time(),"
";
echo date("Y-m-d H:i:s",time()),"
";
echo '一天后的时间为:',date("Y-m-d H:i:s",time()+24*60*60),"
";
echo "一周前的时间为:",date("Y-m-d H:i:s",time()-7*24*60*60),"
";
echo mktime(0,0,0,5,15,2020)."
";
echo date("Y-m-d H:i:s", mktime(0,0,0,5,15,2020))."
";
echo date("Y-m-d H:i:s", mktime(0,0,0,5,15))."
";
echo date("Y-m-d H:i:s", mktime(0,0,0,5))."
";
echo date("Y-m-d H:i:s", mktime(0))."
";
$birth = mktime(0,0,0,11,8,1995);
$now = time();
$age = floor(($now-$birth)/(365*24*60*60));
echo $age,"
";
echo date("Y-m-d H:i:s", time()),"
";
echo date("Y-m-d H:i:s", strtotime("now")),"
";
echo date("Y-m-d H:i:s", strtotime("+1 day")),"
";
echo date("Y-m-d H:i:s", strtotime("-2 weeks")),"
";
echo date("Y-m-d H:i:s", strtotime("+3 years 4 months 5days")),"
";
echo date("Y-m-d H:i:s", strtotime("last Monday")),"
";
微秒
header("content-type:text/html;charset=utf-8;");
echo microtime(),"
";
echo time(),"
";
echo microtime(true),"
";
$start = microtime(true);
for($i=0; $i<100000; $i++){
$arr[] = $i;
}
$end = microtime(true);
echo "程序执行时间为:",round($end-$start, 4);
其它日期时间函数
header("content-type:text/html;charset=utf-8");
print_r(getdate());
echo "
";
print_r(gettimeofday());
echo "
";
var_dump(checkdate(2,3,2008));
echo "
";
var_dump(checkdate(2,30,2008));
H5中新增日期时间相关表单控件
<form action="doAction.php" method="POST">
<input type="date" name="date" id="">type=date<br>
<input type="datetime" name="datetime" id="">type=datetime 未生效<br>
<input type="datetime-local" name="datetime-local" id="">type=datetime-local<br>
<input type="month" name="month" id="">type=month<br>
<input type="week" name="week" id="">type=week<br>
<input type="time" name="time" id="">type=time<br>
<input type="submit" value="提交">
form>