Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply ceiling to a Unix timestamp in PHP?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading