5

我需要在当前时间上增加 15 分钟。例如:现在是 20:48,需要加15分钟,所以现在是21:03,但我需要设置21:15,即它应该是的倍数15,30,45,00。帮助帮助/指导会很有帮助。

<?php
$current_date_time = date('d/m/Y H:i:s');
$current_date = date("d/m/Y H:i", strtotime($current_date_time."+15 minutes"));
echo $current_date;exit;
4

4 回答 4

15

这是一个简单的例子

//what time is it?
$t=time();

//how long is our interval?
$interval=15*60;

//we can calculate when the last interval started by subtracting $t % $interval
$last = $t - $t % $interval;

//so now we know when the next interval will start...
$next = $last + $interval;

echo "Next interval at ".strftime('%H:%M:%S', $next)."\n";

您看起来可能想将 2*$interval 添加到最后一个间隔,但您应该能够调整它以适合您。

于 2013-06-10T11:07:50.030 回答
2

只是为了更正我的帖子:

    $time = time();
    $last_time = ($time - ($time % (15 * 60)));
    $in15mins =  $last_time+ (15 * 60);
    echo 'Now:        '. date('Y-m-d H:i') ."\n";
    echo 'in 15 mins: '. date('Y-m-d H:i', $in15mins) ."\n";

我认为这很自我解释。优化它,使用它。

于 2013-06-10T11:04:53.480 回答
1
$current_date_time = date('d/m/Y H:i:s');
echo $current_date_time."<br>";
$current_date = date("d/m/Y H:i", strtotime($current_date_time."+15 minutes"));
echo $current_date."<br>";
$minutes = date("i",strtotime($current_date));
$min = '';
if($minutes > 0 && $minutes <15){
    $min = 15 - $minutes;
} else if($minutes > 15 && $minutes <30){
       $min = 30 - $minutes;
} else if($minutes > 30 && $minutes <45){
       $min = 45 - $minutes;
} else {
       $min = 59 - $minutes;
       $min++;
}

$newdate = date("d/m/Y H:i", strtotime($current_date."+".$min." minutes"));

echo $newdate;

使用上面的代码。这对我有用。

于 2013-06-10T11:13:46.403 回答
-1
$currentTimeStamp=strtotime('now');
$nextInterval=date("Y-m-d H:i", strtotime("+15 minutes", $currentTimeStamp));
dump($nextInterval);
于 2016-11-04T07:38:09.303 回答