添加15分钟到当前时间并按15分钟间隔

时间:2013-06-10 11:02:21

标签: php datetime

我需要在当前时间增加15分钟。 例如:现在是20:48,需要添加15分钟,所以现在它将是21:03,但我需要设置21:15,也就是说,它应该是{{1}的倍数}。 帮助/指导将有很大帮助。

15,30,45,00

4 个答案:

答案 0 :(得分: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 * $的间隔,但您应该能够根据自己的需要进行调整。

答案 1 :(得分: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";

我认为这是非常自我解释。优化它,使用它。

答案 2 :(得分: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;

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

答案 3 :(得分:0)

$currentTimeStamp=strtotime('now');
$nextInterval=date("Y-m-d H:i", strtotime("+15 minutes", $currentTimeStamp));
dump($nextInterval);
相关问题