PHP基础

获取时间戳

时间戳是指1970年01月01日 00:00:00到这一瞬间经过的秒数

php日期时间函数

time() 返回当前时间的 Unix 时间戳
microtime() 函数返回当前 Unix 时间戳的微秒数
microtime(true)

microtime() 如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,
其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,
msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
如果给出了get_as_float参数并且其值等价于 TRUE,microtime()将返回一个浮点数。

$start = microtime(true);
usleep(100);//程序延迟100毫秒执行
$end = microtime(true);
echo $start."/".$end;
$res = $end - $start;
echo "<br/>";
echo $res."<br/>";

时间戳格式化

date格式化参数

date(format,timestamp); //格式化本地日期和时间。
gmdate(format,timestamp); 格式化 GMT/UTC 日期和时间

timestamp默认为当前时间戳

echo date('H:i:s',time()+7*60*60); //本地时间戳格式化

解析检测日期

$my = getdate();
print_r("$my[month]"); //获取月份
$my_t=getdate(date("U"));
print("$my_t[hours]:$my_t[minutes]:$my_t[seconds],$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]");



mktime(0,0,0,10,3,1975) // 生成时间戳
strtotime('now'); 将英文文本日期时间解析为 Unix 时间戳:

秒转换成时间格式

function changeTimeType($seconds){
	if($seconds>3600){
		$hours   =intval($seconds/3600);
		$minutes =$seconds%3600;
		$time    =$hours.":".gmstrftime('%M:%S',$minutes);
	}else{
		$time    =gmstrftime('%H:%M:%S',$seconds);
	}
	return$time;
}

$seconds = 3612;
echo changeTimeType($seconds);