我从MYSQL获取日期时间,如下所示:
2010-08-11 11:18:28
我需要将其转换为“floor”或最早的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)有点意外……
解决方法:
这会有用吗?是不是有一些功能可以做得更好?
echo floor(strtotime('2010-08-11 11:18:28')/(60*15))*60*15*1000;
1281505500000
Wednesday, August 11, 2010 11:15:00 AM
echo floor(strtotime('2010-08-11 11:28:28')/(60*15))*60*15*1000;
1281505500000
Wednesday, August 11, 2010 11:15:00 AM
echo floor(strtotime('2010-08-11 00:00:00')/(60*15))*60*15*1000;
1281465000000
Wednesday, August 11, 2010 12:00:00 AM
echo floor(strtotime('2010-08-11 23:59:59')/(60*15))*60*15*1000;
1281550500000
Wednesday, August 11, 2010 11:45:00 PM