获取一小时内前8个季度、一小时前

发布于 2024-12-20 03:40:11 字数 290 浏览 1 评论 0原文

我想做的是显示一小时内、一小时前的最后 8 个季度。

我已经有了这个:

<?php
    echo date('d-m-Y H:i:s', strtotime('-1 hour'));
?>

例如:
现在是 14:46(下午 2:46),所以我想要的是:

13:45
13:30
13:15
13:00
12:45
12:30
12:15
12:00

我怎样才能得到它?

What i want to do, is display the last 8 quarters in an hour, one hour ago.

I already have this:

<?php
    echo date('d-m-Y H:i:s', strtotime('-1 hour'));
?>

For example:
Its now 14:46 (2:46 PM), so what i want, is this:

13:45
13:30
13:15
13:00
12:45
12:30
12:15
12:00

How can i get that?

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

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

发布评论

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

评论(4

孤城病女 2024-12-27 03:40:11
<?php
$hour = date('G',(time()-(60*60)));
if (date('i') < 15)
    $min = array('00','45','30','15');
elseif (date('i') < 30)
    $min = array('15','00','45','30');
elseif (date('i') < 45)
    $min = array('30','15','00','45');
else
    $min = array('45','30','15','00');
for ($i=0;$i<=8;$i++) {
    if ($min[$i%4] == 45) {
        if ($hour == 0)
            $hour = 23;
        else 
            $hour--;
    }
    echo $hour.':'.$min[$i%4];
    echo '<br />';
}

现在可以工作,但我还没有进一步测试。

当地时间:15:06

输出:

14:00
13:45
13:30
13:15
13:00
12:45
12:30
12:15
12:00
<?php
$hour = date('G',(time()-(60*60)));
if (date('i') < 15)
    $min = array('00','45','30','15');
elseif (date('i') < 30)
    $min = array('15','00','45','30');
elseif (date('i') < 45)
    $min = array('30','15','00','45');
else
    $min = array('45','30','15','00');
for ($i=0;$i<=8;$i++) {
    if ($min[$i%4] == 45) {
        if ($hour == 0)
            $hour = 23;
        else 
            $hour--;
    }
    echo $hour.':'.$min[$i%4];
    echo '<br />';
}

Works right now, but I have not tested any further.

Local time: 15:06

Output:

14:00
13:45
13:30
13:15
13:00
12:45
12:30
12:15
12:00
梦里南柯 2024-12-27 03:40:11

要解决这个问题,您需要弄清楚上个季度是什么时候。然后从那里返回就很容易了。我会这样做:

$minute = idate('i');
$qdiff = $minute % 15;
$lastQuarter = time() - $qdiff*60;
echo date('Y-m-d H:i', $lastQuarter);

尝试找出我的代码在做什么。特别是学习如何使用 modulo 运算符,因为它对于大量数字问题非常有用!

To solve this problem, you need to figure out when the last quarter was. Then it's as easy as going back from there. Here's how I would do it:

$minute = idate('i');
$qdiff = $minute % 15;
$lastQuarter = time() - $qdiff*60;
echo date('Y-m-d H:i', $lastQuarter);

Try to figure out what my code is doing. Especially learn how to use the modulo operator since it's very useful for a large set of numeric problems!

青衫儰鉨ミ守葔 2024-12-27 03:40:11

或者另一种方法来做到这一点,只是我快速组合起来的东西:

function quarters($date) 
{
    $date = new DateTime($date);
    $date->modify("-1 hour");
    $quarters = array()
    for($i = 0; $i<=7; $i++)
    {
        $nearestQuarter =  floor($date->format("i") / 15) * 15;
        if($i == 0)
        {
            $minuteDiff  = (int) $nearestQuarter - $date->format("i");
            $date->modify(sprintf("%s minutes",$minuteDiff));
        }
        else
        {
            $date->modify("-15 minutes");
        }
        $quarters[] = $date->format("H:i"); 
    }
    return $quarters;
}
var_dump(quarters("2011-12-07 14:46:00"));

输出:

array(8) {
  [0]=>
  string(5) "13:45"
  [1]=>
  string(5) "13:30"
  [2]=>
  string(5) "13:15"
  [3]=>
  string(5) "13:00"
  [4]=>
  string(5) "12:45"
  [5]=>
  string(5) "12:30"
  [6]=>
  string(5) "12:15"
  [7]=>
  string(5) "12:00"
}

Or another way to do it, just something i quickly put together:

function quarters($date) 
{
    $date = new DateTime($date);
    $date->modify("-1 hour");
    $quarters = array()
    for($i = 0; $i<=7; $i++)
    {
        $nearestQuarter =  floor($date->format("i") / 15) * 15;
        if($i == 0)
        {
            $minuteDiff  = (int) $nearestQuarter - $date->format("i");
            $date->modify(sprintf("%s minutes",$minuteDiff));
        }
        else
        {
            $date->modify("-15 minutes");
        }
        $quarters[] = $date->format("H:i"); 
    }
    return $quarters;
}
var_dump(quarters("2011-12-07 14:46:00"));

Output:

array(8) {
  [0]=>
  string(5) "13:45"
  [1]=>
  string(5) "13:30"
  [2]=>
  string(5) "13:15"
  [3]=>
  string(5) "13:00"
  [4]=>
  string(5) "12:45"
  [5]=>
  string(5) "12:30"
  [6]=>
  string(5) "12:15"
  [7]=>
  string(5) "12:00"
}
凉世弥音 2024-12-27 03:40:11

我建议它不需要那么多代码来完成您需要的操作:

$t = time() - 60 * 60;
$t -= $t % (60 * 15);

for ($i = 0; $i < 8; $i++, $t -= (60 * 15)) {
  echo date('H:i', $t), "\n";
}

I suggest it doesn't need anywhere near so much code to do what you need:

$t = time() - 60 * 60;
$t -= $t % (60 * 15);

for ($i = 0; $i < 8; $i++, $t -= (60 * 15)) {
  echo date('H:i', $t), "\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文