0

我经常习惯于获得下一个每日事件的时间,但是更短的时间间隔呢?strtotime("next time")

每次分钟的个位为 7 时都会发生一些事情(以10 分钟为间隔00:07,以此类推)00:1700:27

因此,鉴于当前时间,我如何才能获得下一个时间?

目前我能想到的最好的是strtotime(substr(date("Y-m-d H:i"),0,-1)."7"),但它似乎有点脏,甚至在 xx:x7 和 xx:x0 之前的时间都不起作用。有没有更好的办法?

4

3 回答 3

0

支持的用法DateTime

<?php
$dt = new DateTime('2013-01-01 16:54:11');
for($qq = 0; $qq < 10; ++$qq) { // loop to test all minute remainders
    $dt->modify('+1 minute +3seconds'); // seconds just for show
    $min = +$dt->format('i'); // current minutes
    $sec = +$dt->format('s'); // current seconds
    $r10 = $min % 10;
    // if it's hh:27 now, this will result in hh:37, change >= to > if that forwarding is not needed
    if($r10 >= 7) {
        $deltaMin = 17 - $r10;
    } else {
        $deltaMin = 7 - $r10;
    }
    // time left until next "good" point in time
    $change = sprintf('%+d minutes -%d seconds', $deltaMin, $sec);
    $new = clone($dt);
    $new->modify($change);
    printf("%s\t %s\n%s\n--\n", $dt->format('r'), $change, $new->format('r'));
}
于 2013-01-01T15:58:29.327 回答
0

你快到了:

$now = time() + (date('is')>5700?600:0);
$new = strtotime(substr(date("Y-m-d H:i", $now),0,-1)."7");
于 2013-01-01T16:15:15.567 回答
0

一种选择是使用该DateTime::setTime()方法,并带有一点算术。

$date = new DateTime('12:34', new DateTimeZone('Europe/Paris'));

$minute = ceil(($date->format('i') - 7 + 1) / 10) * 10 + 7;
$date->setTime($date->format('G'), $minute, 0);

echo $date->format('H:i'); // 12:37

出于演示目的,在循环中使用它的示例:

$date = new DateTime('16:00', new DateTimeZone('Europe/Paris'));
$period = new DatePeriod($date, new DateInterval('PT1M'), 60);
foreach ($period as $date) {
    echo $date->format('H:i => ');

    $minute = ceil(($date->format('i') - 7 + 1) / 10) * 10 + 7;
    $date->setTime($date->format('G'), $minute, 0);

    echo $date->format('H:i'), PHP_EOL;
}

上面的输出类似于:

16:00 => 16:07
16:01 => 16:07
16:02 => 16:07
16:03 => 16:07
16:04 => 16:07
16:05 => 16:07
16:06 => 16:07
16:07 => 16:17
16:08 => 16:17
16:09 => 16:17
16:10 => 16:17
... removed to save scrolling ...
16:50 => 16:57
16:51 => 16:57
16:52 => 16:57
16:53 => 16:57
16:54 => 16:57
16:55 => 16:57
16:56 => 16:57
16:57 => 17:07
16:58 => 17:07
16:59 => 17:07
17:00 => 17:07

» 查看这个在线运行的示例

于 2013-01-01T18:42:23.130 回答