Question

What is the correct way to round down current time to 5 minutes in PHP minus 5 minutes? We need output time string, everything in 2 decimals in this format:

$today = date("dmYHi");   

Example:

16:32 -> 16:25

18:54 -> 18:45

20:04 -> 19:55

Thanks a lot!

Was it helpful?

Solution

Try this:

$date = new DateTime();
$hours = $date->format('H')
$minutes = (integer) $date->format('i');

// round down to the nearest multiple of 5
$minutes = floor($minutes / 5 ) * 5;

// if $minutes is 0 or 5 we add a trailing 0
if($minutes < 10) {
    $minutes = '0' . $minutes;
}

// output
echo $hours . ':' . $minutes;

OTHER TIPS

    $current_time = time();
    $time = $current_time - (5 * 60);
    $time -= $time % (60 * 5);
    print_r( date("H:i", $current_time) . " -> " . date("H:i", $time) );

Output: 11:19 -> 11:10

$time = time();
$time -= $time / 60 % 5 * 5 + 300;

$today = date("dmYHi", $time);
print "$today\n";

print date('H:i', $time);

Try this:

date_default_timezone_set('Asia/Jerusalem');

//Convert your time to unixtimestamp
$time = strtotime('20:04');


echo date('Y-m-d H:i:s', $time);

//Calculate by 5mins round down
$time = intval($time/300)*300-300;

echo date('Y-m-d H:i:s', $time);

Try this:

$h=date("H");
$m=date("i")-5;

while($m%5!=0){
if($m==0)
$h--;
$m--;
}

echo "$h:$m";
$hours = date('H');
$minutes = (integer) date('i');

// round down to the nearest multiple of 5
$minutes = floor($minutes / 5 ) * 5 - 5;

// if $minutes is 0 or 5 we add a trailing 0
if($minutes < 10) {
$minutes = '0' . $minutes;
}
$date = date("dmY");  

#Variablen maken voor 60 minuten radar loop
$a = 'ref_' . $date . $hours . ($minutes - 55 ) . '.png';
$b = 'ref_' . $date . $hours . ($minutes - 50 ) . '.png';
$c = 'ref_' . $date . $hours . ($minutes - 45 ) . '.png';
$d = 'ref_' . $date . $hours . ($minutes - 40 ) . '.png';
$e = 'ref_' . $date . $hours . ($minutes - 35 ) . '.png';
$f = 'ref_' . $date . $hours . ($minutes - 30 ) . '.png';
$g = 'ref_' . $date . $hours . ($minutes - 25 ) . '.png';
$h = 'ref_' . $date . $hours . ($minutes - 20 ) . '.png';
$i = 'ref_' . $date . $hours . ($minutes - 15 ) . '.png';
$j = 'ref_' . $date . $hours . ($minutes - 10 ) . '.png';
$k = 'ref_' . $date . $hours . ($minutes - 5 ) . '.png';
$l = 'ref_' . $date . $hours . $minutes . '.png';

print $a .'<br />' . $b .'<br />' . $c .'<br />' . $d .'<br />' . $e .'<br />' . $f .'<br />' . $g .'<br />' . $h .'<br />' . $i .'<br />' . $j .'<br />' . $k .'<br />' . $l;

This outputs:

ref_0604201422-50.png
ref_0604201422-45.png
ref_0604201422-40.png
ref_0604201422-35.png
ref_0604201422-30.png
ref_0604201422-25.png
ref_0604201422-20.png
ref_0604201422-15.png
ref_0604201422-10.png
ref_0604201422-5.png
ref_06042014220.png
ref_060420142205.png

It should output:

ref_060420142110.png
ref_060420142115.png
ref_060420142120.png
ref_060420142125.png
ref_060420142130.png
ref_060420142135.png
ref_060420142140.png
ref_060420142145.png
ref_060420142150.png
ref_060420142155.png
ref_060420142200.png
ref_060420142205.png

thank you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top