Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round up current time to nearest 15 minutes using PHP [duplicate]

Tags:

php

time

Is there a simple way of getting the current time and rounding it up to the next quarter of an hour?

For example if it is 12:36 then display 12:45 if it's 13:01 then display 13:15

etc

like image 202
user3305539 Avatar asked May 22 '14 11:05

user3305539


1 Answers

here is your answer

$current_date = date('d-M-Y g:i:s A');
echo $current_date."<br/>";
$current_time = strtotime($current_date);

$frac = 900;
$r = $current_time % $frac;

$new_time = $current_time + ($frac-$r);
$new_date = date('d-M-Y g:i:s A', $new_time);

echo $new_date."<br/>";

LIVE DEMO

like image 56
Satish Sharma Avatar answered Oct 13 '22 00:10

Satish Sharma