I have a list of dates.
How can I round, for example the todays date, to the nearest date on the list?
Round date to nearest given date
Moderator: General Moderators
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
Re: Round date to nearest given date
1. put the dates in an array
2. cycle the array, find the difference (PHP can return result in seconds) between today and the date
3. the minimum difference is associated with the closest date
Can you code the above?
Of course, you can do the calculation in days, hrs... whatever you decide
2. cycle the array, find the difference (PHP can return result in seconds) between today and the date
3. the minimum difference is associated with the closest date
Can you code the above?
Of course, you can do the calculation in days, hrs... whatever you decide
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
Re: Round date to nearest given date
You are right.
But if I want to find the nearest hour date("H:i"). Let's skip the dates, just for the hours.
But if I want to find the nearest hour date("H:i"). Let's skip the dates, just for the hours.
Re: Round date to nearest given date
Convert them to UNIX timestamps & you can use whatever metric ( > 1 second ) you want
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.