挺有意思:PHP 将N秒内连续的记录视为一条记录
现在时间是11:34:00秒,我向系统发送了一条 “hello,”,
02秒 – “i”
03秒 – “am”
04秒 – “php_yt”
当发送 “hello” 时,系统保存了一条记录sendtime:1638589060,text:hello,
,
5秒内系统视为一条记录,即sendtime:1638589060,text:hello, i am php_yt
通过下面的可实现
$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,
相同的时间
当然也可以调整 5 秒为 10 秒,原理就是
echo $now / 5; //327717906.6 每秒小数点向前 0.2,取整忽略掉
10 秒的话,小数点向前 0.1,取整忽略掉。
不过上面的有 bug,可将一段时间内的发送时间都转为 custom_time
合并,具体应用场景不详,权当娱乐
本作品采用《CC 协议》,转载必须注明作者和本文链接