Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 15 minutes to current time and snap to 15 minute intervals

Tags:

php

datetime

I need to add 15 minutes to the current time. For eg : now is 20:48 , need to add 15 minutes, so now it will be 21:03, but i need to set 21:15 ,that is , it should be in multiples of15,30,45,00. Help help/guidance would be of good help.

<?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;
like image 341
Vimalnath Avatar asked Jun 10 '13 11:06

Vimalnath


2 Answers

Here's a simple example

//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";

You look like you might want to add 2*$interval to the last interval, but you should be able to adapt this to suit you.

like image 138
Paul Dixon Avatar answered Nov 06 '22 11:11

Paul Dixon


Just to correct my post:

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

I think it is quite self explaining. Optimize it, use it.

like image 2
Langusten Gustel Avatar answered Nov 06 '22 10:11

Langusten Gustel