将 MYSQL 日期时间四舍五入到最早的 15 分钟间隔(以毫秒为单位)(PHP)

发布于 2024-09-14 12:45:03 字数 1585 浏览 2 评论 0原文

我正在从 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) 有点令人惊讶......

I'm fetching a datetime from MYSQL which looks like:

2010-08-11 11:18:28

I need to convert it into the "floor" or the earliest 15 minute interval and output in milliseconds for another function.

So, this case would be:

2010-08-11 11:15:00 in milliseconds


Whoops! Sorry - need to clarify - I need code that will transform it into milliseconds WITHIN php!


Doing a timing test revealed the following:

$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);

time taken = 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);

time taken = 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);

time taken = 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);

time taken = 20.687357902527

time taken = 19.32729101181

time taken = 19.938629150391

It appears that the strtotime() function is a slow one and we probably should avoid using it doubly for every time its required. The timetaken(60*15) != timetaken(900) was a little bit of a surprise...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一瞬间的火花 2024-09-21 12:45:03

这行得通吗?难道没有一些功能可以做得更好吗?

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

Would this work? Isn't there some function that would do this better?

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
嗼ふ静 2024-09-21 12:45:03

以下将为您提供最早的 15 分钟间隔:

select UNIX_TIMESTAMP(YOURDATETIME)-MOD(UNIX_TIMESTAMP(YOURDATETIME), 900) 
from YOURTABLE

结果以纪元秒为单位(自 '1970-01-01 00:00:00' UTC 以来的秒数),如果您想要该值以毫秒为单位,则应乘以 1000(在查询或 PHP 中,如您认为合适)。

编辑

在 PHP 中,您的值集为:

$values = array('2010-08-11 11:18:28', '2010-08-11 11:28:28', '2010-08-11 00:00:00', '2010-08-11 23:59:59');
foreach($values as $value) {
    $result = (strtotime($value)-(strtotime($value) % 900));
    echo "$value -> $result\n";
}

当您将 $result 乘以 1000(以毫秒为单位获取值)时,您可能会出现溢出,结果将转换为浮点型。这可能不是您想要的,因此您最好使用 bcmul:

$result = bcmul((strtotime($value)-(strtotime($value) % 900)), 1000);

The following will give you the earliest 15 minute interval :

select UNIX_TIMESTAMP(YOURDATETIME)-MOD(UNIX_TIMESTAMP(YOURDATETIME), 900) 
from YOURTABLE

The result is in epoch seconds (seconds since '1970-01-01 00:00:00' UTC), if you want that value in milliseconds you should multiply by 1000 (either in the query or in PHP, as you see fit).

EDIT

In PHP for your set of values that would be :

$values = array('2010-08-11 11:18:28', '2010-08-11 11:28:28', '2010-08-11 00:00:00', '2010-08-11 23:59:59');
foreach($values as $value) {
    $result = (strtotime($value)-(strtotime($value) % 900));
    echo "$value -> $result\n";
}

When you multiply $result by 1000 (to get the value in ms) you will probably get an overflow and the result will be converted to a float. This is probably not what you want, so you might be better of using bcmul :

$result = bcmul((strtotime($value)-(strtotime($value) % 900)), 1000);
无边思念无边月 2024-09-21 12:45:03

尝试一下,看看会发生什么:

select DATE(myColumn) 
       + CONVERT(HOUR(myColumn),CHAR(2)) + ':'
       + CASE 
         WHEN MINUTE(myColumn) < 15 THEN '00'
         WHEN MINUTE(myColumn) < 30 THEN '15'
         WHEN MINUTE(myColumn) < 45 THEN '30'
         ELSE '45'
       END 
       + ':00' as myDate

Give this a try and see what happens:

select DATE(myColumn) 
       + CONVERT(HOUR(myColumn),CHAR(2)) + ':'
       + CASE 
         WHEN MINUTE(myColumn) < 15 THEN '00'
         WHEN MINUTE(myColumn) < 30 THEN '15'
         WHEN MINUTE(myColumn) < 45 THEN '30'
         ELSE '45'
       END 
       + ':00' as myDate
内心激荡 2024-09-21 12:45:03

好吧,让我们看看最糟糕的解决方案:

<?php

$ts    = explode(' ', '2010-08-11 11:18:28');
$date  = explode('-', $ts[0]);
$time  = explode(':', $ts[1]);
$ts    = mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]);
$floor = mktime($time[0], round($time[1]/15)*15, 0, $date[1], $date[2], $date[0]);

Ok, let's see the worst solution:

<?php

$ts    = explode(' ', '2010-08-11 11:18:28');
$date  = explode('-', $ts[0]);
$time  = explode(':', $ts[1]);
$ts    = mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]);
$floor = mktime($time[0], round($time[1]/15)*15, 0, $date[1], $date[2], $date[0]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文