php日期转微秒,使用PHP生成独特的微秒级
PHP中的time()函數(shù)返回當(dāng)前的Unix時(shí)間戳, 這是從 Unix時(shí)代的秒數(shù)開始測(cè)量的時(shí)間, 在某些情況下非常有用,但并非總是如此。與大多數(shù)其他PHP函數(shù)一樣,此函數(shù)也是跨平臺(tái)兼容的,因?yàn)樗m用于Unix,Linux,Windows和Mac。
PHP函數(shù)的 microtime()更精確和更精細(xì),因?yàn)樗晕⒚敕祷禺?dāng)前的Unix時(shí)間戳, 我們的問題是它返回一個(gè)包含空格和點(diǎn)的字符串,例如,如果您從PHP中生成文件名或HTML或CSS標(biāo)識(shí)符的一部分,那么這個(gè)字符串不是很有用。
這就是為什么我們?cè)谙旅鎸懥诉@么個(gè)小而有用的函數(shù), 它基于PHP函數(shù) microtime (),但是,返回一個(gè)干凈的數(shù)字字符串。
請(qǐng)享用!<?php /**
* Generates and returns a string of digits representing the time of the
* current system in microseconds granularity.
*
* Compared to the standard time() function, the microtime() function is more
* accurate and in addition, successive quick calls inside a loop generate
* unique results; which can be quite useful in certain cases.
*
* Our function below generates digits only output based on the time stamp
* generated by the microtime() function.
*
* @return string
*/functionget_clean_microtimestamp_string() {//Get raw microtime (with spaces and dots and digits)$mt=microtime();//Remove all non-digit (or non-integer) characters$r="";$length=strlen($mt);
for($i=0;$i
if(ctype_digit($mt[$i])) {$r.=$mt[$i];
}
}//Returnreturn$r;
}
注意,microtime()僅在支持gettimeofday()系統(tǒng)調(diào)用的操作系統(tǒng)上可用, 我們?cè)?Windows 7. Windows 8 和Ubuntu14上測(cè)試了它,它們上都能正常工作。
注意microtime()會(huì)產(chǎn)生一個(gè)不同的輸出值,即使是連續(xù)多次調(diào)用, 你自己試試吧, 或者,從命令行使用下面的PHP代碼,看看我們?nèi)绾螠y(cè)試函數(shù)。<?phpfunctionarray_has_duplicates ($array) {
returncount($array)!==count(array_unique($array));
}$microtimes= array();
for($i=0;$i<1000;$i++) {$microtimes[] =get_clean_microtimestamp_string();
}
foreach($microtimesas$microtime) {
echo$microtime."n";
}
if(array_has_duplicates($microtimes)) {
echo"FOUND DUPLICATES!n";
}
else {
echo"NO DUPLICATES FOUND. AWESOME!n";
}
總結(jié)
以上是生活随笔為你收集整理的php日期转微秒,使用PHP生成独特的微秒级的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 科学课和计算机整合,信息技术与科学课整合
- 下一篇: Centos7安装php7.4