tags:

views:

316

answers:

2

Hi,

Assuming I have a unix timestamp in PHP. How can I round my php timestamp to the nearest minute? E.g. 16:45:00 as opposed to 16:45:34?

Thanks for your help! :)

+1  A: 

If the timestamp is a Unix style timestamp, simply

$rounded = round($time/60)*60;

If it is the style you indicated, you can simply convert it to a Unix style timestamp and back

$rounded = date('H:i:s', round(strtotime('16:45:34')/60)*60);

round() is used as a simple way of ensuring it rounds to x for values between x - 0.5 <= x < x + 0.5. If you always wanted to always round down (like indicated) you could use floor() or the modulo function

$rounded = floor($time/60)*60;
//or
$rounded = time() - time() % 60;
Yacoby
Yacoby, thanks for the comprehensive explanation! Now that you've mentioned it, round() is the function I prefer to have. Makes more sense in my application. :)
Lyon
+1  A: 

Ah dam. Beat me to it :)

This was my solution also.

<?php 
$round = ( round ( time() / 60 ) * 60 );

echo date('h:i:s A', $round );
?>

http://php.net/manual/en/function.time.php

Laykes
hehe, i really appreciate your help! thanks :)
Lyon