我正在从 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) 有点令人惊讶......
这行得通吗?难道没有一些功能可以做得更好吗?
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)