Home > Software engineering >  How to apply ceiling to a Unix timestamp in PHP?
How to apply ceiling to a Unix timestamp in PHP?

Time:03-01

I have a timestamp like 3:07:01 pm. Regardless of the number of seconds, I always need to round up this timestamp to 3:08:00 pm. How can I achieve this? I have tried using the ceiling function like so:

$time_rounded_up_to_nearest_minute = ceil(round(time() / 60) * 60);

but this function gives me some weird behavior around the 30 second mark. As soon as 30s mark hits it goes to the next minute, which is not what I want. I want it to always go to the nearest minute regardless of seconds

CodePudding user response:

Don't use round(), since it rounds to the nearest integer, it doesn't round up. Just use ceil() by itself.

$time_rounded_up_to_nearest_minute = ceil(time() / 60) * 60;
  • Related