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! :)
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! :)
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;
Ah dam. Beat me to it :)
This was my solution also.
<?php
$round = ( round ( time() / 60 ) * 60 );
echo date('h:i:s A', $round );
?>