What would be a good solution to round a timestamp to the hour?
For example, after using date('d.m.y h:m', time()), I get ‘02.08.11 7:07’… Now, from this, I’d like to have the timestamp for 7:00.
How can I achieve that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Update
After seeing Greg Miller’s “Time Programming Fundamentals” talk about Google’s cctz C++ library, I am dissatisfied with my previous answer.
Handling time is a very complex topic (even when discounting relativistic effects). We have to honor things like timezones with uneven offsets, daylight saving time, time jumps due to legislation changes or leap seconds and probably many more quirks.
While many of these have only a local effect in time and space, users of our programs can still be unlucky enough to get affected.
TL;DR
It is probably best to rely on underlying functions like in holodoc’s answer to handle the quirks of timezone management.
Old Answer
When I remember correctly the timestamp started at exactly 0:00 1.1.1970 so it should suffice to divide it by 3600 (floating) then round/floor/ceil and multiply by 3600 (integer). The division must be floating point for round/ceil to work.
Sample of rounding:
Sample of flooring:
Sample of ceiling: