挺有意思:PHP 將N秒內(nèi)連續(xù)的記錄視為一條記錄~
現(xiàn)在時間是 11:34:00 秒,我向系統(tǒng)發(fā)送了一條 “hello,”,
02 秒 – “i” 03 秒 – “am” 04 秒 – “php_yt”
當(dāng)發(fā)送 “hello” 時,系統(tǒng)保存了一條記錄
sendtime:1638589060,text:hello,,
5 秒內(nèi)系統(tǒng)視為一條記錄,即
sendtime:1638589060,text:hello, i am php_yt
通過下面的可實現(xiàn)
$now = time(); $now2 = intval( $now /5 ) * 5;
測試代碼
echo $now = time();//1638589533 echo PHP_EOL; echo $now / 5; //327717906.6 echo PHP_EOL; echo $custom_time = intval( $now /5 ) * 5;//1638589530
假如 hello, 這條記錄的時間是 1638589530 , 那么目前時間戳 1638589533 也視為和 hello, 相同的時間
當(dāng)然也可以調(diào)整 5 秒為 10 秒,原理就是
echo $now / 5; //327717906.6 每秒小數(shù)點向前 0.2,取整忽略掉
10 秒的話,小數(shù)點向前 0.1,取整忽略掉。
不過上面的有 bug,可將一段時間內(nèi)的發(fā)送時間都轉(zhuǎn)為 custom_time 合并,具體應(yīng)用場景不詳,權(quán)當(dāng)娛樂
推薦學(xué)習(xí):《PHP視頻教程》