我正在从 MYSQL 获取日期时间,如下所示:
2010-08-11 11:18:28
我需要将其转换为“地板”或最早的 15 分钟间隔,并以毫秒为单位输出另一个功能。
因此,这种情况将是:
2010-08-11 11:15:00 以毫秒为单位
哎呀!抱歉 - 需要澄清 - 我需要将其转换为 php 内毫秒的代码!
进行时序测试显示以下内容:
$time_start = microtime(true);
for($i=0;$i<10000;$i++)
floor(strtotime('2010-08-11 23:59:59')/(60*15))*60*15*1000;
$time_end = microtime(true);
echo 'time taken = '.($time_end - $time_start);
所用时间 = 21.440743207932
$time_start = microtime(true);
for($i=0;$i<10000;$i++)
strtotime('2010-08-11 23:59:59')-(strtotime('2010-08-11 23:59:59') % 900);
$time_end = microtime(true);
echo 'time taken = '.($time_end - $time_start);
所用时间 = 39.597450017929
$time_start = microtime(true);
for($i=0;$i<10000;$i++)
bcmul((strtotime('2010-08-11 23:59:59')-(strtotime('2010-08-11 23:59:59') % 900)), 1000);
$time_end = microtime(true);
echo 'time taken = '.($time_end - $time_start);
所用时间 = 42.297260046005
$time_start = microtime(true);
for($i=0;$i<10000;$i++)
floor(strtotime('2010-08-11 23:59:59')/(900))*900000;
$time_end = microtime(true);
echo 'time taken = '.($time_end - $time_start);
所用时间 = 20.687357902527
所用时间 = 19.32729101181
所用时间 = 19.938629150391
看来 strtotime() 函数很慢,我们可能应该避免在每次需要时双重使用它。timetaken(60*15) != timetaken(900) 有点令人惊讶......